ProjectionManager   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 20.25 %

Importance

Changes 0
Metric Value
dl 16
loc 79
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A rollback() 7 7 2
A __construct() 0 3 1
A project() 7 7 2
A rollbackAnEventAggregate() 0 4 2
A register() 0 4 2
A projectFromAnEventAggregate() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the Simple EventStore Manager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
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
namespace SimpleEventStoreManager\Infrastructure\Projector;
12
13
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface;
14
use SimpleEventStoreManager\Domain\Model\Contracts\EventStoreRepositoryInterface;
15
use SimpleEventStoreManager\Domain\Model\EventAggregate;
0 ignored issues
show
Bug introduced by
The type SimpleEventStoreManager\...in\Model\EventAggregate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SimpleEventStoreManager\Domain\Model\AggregateUuid;
17
use SimpleEventStoreManager\Infrastructure\Projector\Exceptions\ProjectorDoesNotExistsException;
18
19
class ProjectionManager
20
{
21
    /**
22
     * @var Projector[]
23
     */
24
    private $projectors;
25
26
    /**
27
     * @var EventStoreRepositoryInterface
28
     */
29
    private $repo;
30
31
    /**
32
     * ProjectionManager constructor.
33
     * @param EventStoreRepositoryInterface $repo
34
     */
35
    public function __construct(EventStoreRepositoryInterface $repo)
36
    {
37
        $this->repo = $repo;
38
    }
39
40
    /**
41
     * @param Projector $projector
42
     */
43
    public function register(Projector $projector)
44
    {
45
        foreach ($projector->subcribedEvents() as $subcribedEvent) {
46
            $this->projectors[$subcribedEvent] = $projector;
47
        }
48
    }
49
50
    /**
51
     * @param EventInterface $event
52
     * @throws ProjectorDoesNotExistsException
53
     */
54 View Code Duplication
    public function project(EventInterface $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if (!isset($this->projectors[get_class($event)])) {
57
            throw new ProjectorDoesNotExistsException('No Projector found for event ' . get_class($event) . '.');
58
        }
59
60
        $this->projectors[get_class($event)]->handle($event);
61
    }
62
63
    /**
64
     * @param AggregateUuid $uuid
65
     */
66
    public function projectFromAnEventAggregate(AggregateUuid $uuid)
67
    {
68
        /** @var EventInterface $event */
69
        foreach ($this->repo->byUuid($uuid, EventStoreRepositoryInterface::RETURN_AS_OBJECT) as $event) {
70
            try {
71
                $this->project($event);
72
            } catch (ProjectorDoesNotExistsException $e) {
73
                $this->rollbackAnEventAggregate($uuid);
74
            }
75
        }
76
    }
77
78
    /**
79
     * @param EventInterface $event
80
     * @throws ProjectorDoesNotExistsException
81
     */
82 View Code Duplication
    public function rollback(EventInterface $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (!isset($this->projectors[get_class($event)])) {
85
            throw new ProjectorDoesNotExistsException('No Projector found for event ' . get_class($event) . '.');
86
        }
87
88
        $this->projectors[get_class($event)]->rollback($event);
89
    }
90
91
    /**
92
     * @param AggregateUuid $eventUuid
93
     */
94
    public function rollbackAnEventAggregate(AggregateUuid $eventUuid)
95
    {
96
        foreach ($this->repo->byUuid($eventUuid, EventStoreRepositoryInterface::RETURN_AS_OBJECT) as $event) {
97
            $this->rollback($event);
98
        }
99
    }
100
}
101