-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectionSynchronization.php
More file actions
49 lines (40 loc) · 1.23 KB
/
ProjectionSynchronization.php
File metadata and controls
49 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace MsgPhp\Domain\Projection;
use Psr\Log\LoggerInterface;
/**
* @author Roland Franssen <[email protected]>
*/
final class ProjectionSynchronization
{
private $typeRegistry;
private $repository;
/** @var iterable<string, array> */
private $documentProvider;
private $logger;
/**
* @param iterable<string, array> $documentProvider
*/
public function __construct(ProjectionTypeRegistry $typeRegistry, ProjectionRepository $repository, iterable $documentProvider, ?LoggerInterface $logger = null)
{
$this->typeRegistry = $typeRegistry;
$this->repository = $repository;
$this->documentProvider = $documentProvider;
$this->logger = $logger;
}
public function synchronize(): int
{
$this->typeRegistry->destroy();
$this->typeRegistry->initialize();
$grouped = [];
$synchronized = 0;
foreach ($this->documentProvider as $type => $document) {
$grouped[$type][] = $document;
++$synchronized;
}
foreach ($grouped as $type => $documents) {
$this->repository->saveAll($type, $documents);
}
return $synchronized;
}
}