Completed
Pull Request — master (#349)
by Luc
05:44
created

Projector::applyLabelAdded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Label\ReadModels\JSON;
4
5
use Broadway\Domain\Metadata;
6
use CultuurNet\UDB3\Label\Events\CopyCreated;
7
use CultuurNet\UDB3\Label\Events\Created;
8
use CultuurNet\UDB3\Label\Events\MadeInvisible;
9
use CultuurNet\UDB3\Label\Events\MadePrivate;
10
use CultuurNet\UDB3\Label\Events\MadePublic;
11
use CultuurNet\UDB3\Label\Events\MadeVisible;
12
use CultuurNet\UDB3\Label\ReadModels\AbstractProjector;
13
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\ReadRepositoryInterface;
14
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\WriteRepositoryInterface;
15
use CultuurNet\UDB3\LabelEventInterface;
16
use CultuurNet\UDB3\LabelsImportedEventInterface;
17
use ValueObjects\Identity\UUID;
18
use ValueObjects\StringLiteral\StringLiteral;
19
20
class Projector extends AbstractProjector
21
{
22
    /**
23
     * @var WriteRepositoryInterface
24
     */
25
    private $writeRepository;
26
27
    /**
28
     * @var ReadRepositoryInterface
29
     */
30
    private $readRepository;
31
32
    /**
33
     * Projector constructor.
34
     * @param WriteRepositoryInterface $writeRepository
35
     * @param ReadRepositoryInterface $readRepository
36
     */
37
    public function __construct(
38
        WriteRepositoryInterface $writeRepository,
39
        ReadRepositoryInterface $readRepository
40
    ) {
41
        $this->writeRepository = $writeRepository;
42
        $this->readRepository = $readRepository;
43
    }
44
45
    /**
46
     * @param Created $created
47
     */
48 View Code Duplication
    public function applyCreated(Created $created)
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...
49
    {
50
        $entity = $this->readRepository->getByUuid($created->getUuid());
51
52
        if (is_null($entity)) {
53
            $this->writeRepository->save(
54
                $created->getUuid(),
55
                $created->getName(),
56
                $created->getVisibility(),
57
                $created->getPrivacy()
58
            );
59
        }
60
    }
61
62
    /**
63
     * @param CopyCreated $copyCreated
64
     */
65 View Code Duplication
    public function applyCopyCreated(CopyCreated $copyCreated)
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...
66
    {
67
        $entity = $this->readRepository->getByUuid($copyCreated->getUuid());
68
69
        if (is_null($entity)) {
70
            $this->writeRepository->save(
71
                $copyCreated->getUuid(),
72
                $copyCreated->getName(),
73
                $copyCreated->getVisibility(),
74
                $copyCreated->getPrivacy(),
75
                $copyCreated->getParentUuid()
76
            );
77
        }
78
    }
79
80
    /**
81
     * @param MadeVisible $madeVisible
82
     */
83
    public function applyMadeVisible(MadeVisible $madeVisible)
84
    {
85
        $this->writeRepository->updateVisible($madeVisible->getUuid());
86
    }
87
88
    /**
89
     * @param MadeInvisible $madeInvisible
90
     */
91
    public function applyMadeInvisible(MadeInvisible $madeInvisible)
92
    {
93
        $this->writeRepository->updateInvisible($madeInvisible->getUuid());
94
    }
95
96
    /**
97
     * @param MadePublic $madePublic
98
     */
99
    public function applyMadePublic(MadePublic $madePublic)
100
    {
101
        $this->writeRepository->updatePublic($madePublic->getUuid());
102
    }
103
104
    /**
105
     * @param MadePrivate $madePrivate
106
     */
107
    public function applyMadePrivate(MadePrivate $madePrivate)
108
    {
109
        $this->writeRepository->updatePrivate($madePrivate->getUuid());
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function applyLabelAdded(LabelEventInterface $labelAdded, Metadata $metadata)
116
    {
117
        $uuid = $this->getUuid($labelAdded);
118
119
        if ($uuid) {
120
            $this->writeRepository->updateCountIncrement($uuid);
121
        }
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function applyLabelRemoved(LabelEventInterface $labelRemoved, Metadata $metadata)
128
    {
129
        $uuid = $this->getUuid($labelRemoved);
130
131
        if ($uuid) {
132
            $this->writeRepository->updateCountDecrement($uuid);
133
        }
134
    }
135
136
    /**
137
     * @inheritdoc
138
     */
139
    public function applyLabelsImported(LabelsImportedEventInterface $labelsImported, Metadata $metadata)
140
    {
141
        // This projector does not handle this event, but it is part of abstract projector.
142
    }
143
144
    /**
145
     * @param LabelEventInterface $labelEvent
146
     * @return UUID|null
147
     */
148
    private function getUuid(LabelEventInterface $labelEvent)
149
    {
150
        $uuid = null;
151
152
        $name = new StringLiteral((string) $labelEvent->getLabel());
153
154
        $entity = $this->readRepository->getByName($name);
155
        if ($entity !== null) {
156
            $uuid = $entity->getUuid();
157
        }
158
159
        return $uuid;
160
    }
161
}
162