Passed
Pull Request — master (#4)
by Tomasz
04:04
created

Repository::pullEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 *
4
 * This file is part of the Aggrego.
5
 * (c) Tomasz Kunicki <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
declare(strict_types = 1);
13
14
namespace Aggrego\DomainEventProducer\Domain;
15
16
use Aggrego\Domain\Board\Board;
17
use Aggrego\Domain\Board\Exception\BoardExistException;
18
use Aggrego\Domain\Board\Exception\BoardNotFoundException;
19
use Aggrego\Domain\Board\Repository as DomainRepository;
20
use Aggrego\Domain\Board\Uuid;
21
use Aggrego\EventConsumer\Shared\Events;
22
23
class Repository implements DomainRepository
24
{
25
    /**
26
     * @var DomainRepository
27
     */
28
    private $repository;
29
30
    /**
31
     * @var Uuid[]
32
     */
33
    private $modified = [];
34
35
    public function __construct(DomainRepository $repository)
36
    {
37
        $this->modified = [];
38
        $this->repository = $repository;
39
    }
40
41
    /**
42
     * @param  Uuid $uuid
43
     * @return Board
44
     * @throws BoardNotFoundException
45
     */
46
    public function getBoardByUuid(Uuid $uuid): Board
47
    {
48
        $board = $this->repository->getBoardByUuid($uuid);
49
        $this->modified[] = $uuid;
50
        return $board;
51
    }
52
53
    /**
54
     * @param  Board $board
55
     * @throws BoardExistException
56
     */
57
    public function addBoard(Board $board): void
58
    {
59
        $this->repository->addBoard($board);
60
        $this->modified[] = $board->getUuid();
61
    }
62
63
    public function pullEvents(): Events
64
    {
65
        $events = new Events();
66
        foreach ($this->modified as $uuid) {
67
            foreach ($this->repository->getBoardByUuid($uuid)->pullEvents() as $event) {
68
                $events->add($event);
69
            }
70
        }
71
        $this->modified = [];
72
        return $events;
73
    }
74
}
75