Completed
Pull Request — master (#238)
by Luc
05:08
created

CommandHandler::handleSyncLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Label;
4
5
use Broadway\Repository\RepositoryInterface;
6
use Broadway\UuidGenerator\UuidGeneratorInterface;
7
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler as AbstractCommandHandler;
8
use CultuurNet\UDB3\EventSourcing\DBAL\UniqueConstraintException;
9
use CultuurNet\UDB3\Label;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CultuurNet\UDB3\Label\Label.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use CultuurNet\UDB3\Label\Commands\Create;
11
use CultuurNet\UDB3\Label\Commands\CreateCopy;
12
use CultuurNet\UDB3\Label\Commands\MakeInvisible;
13
use CultuurNet\UDB3\Label\Commands\MakePrivate;
14
use CultuurNet\UDB3\Label\Commands\MakePublic;
15
use CultuurNet\UDB3\Label\Commands\MakeVisible;
16
use CultuurNet\UDB3\Label\Label as LabelAggregate;
17
use CultuurNet\UDB3\Label\ValueObjects\LabelName;
18
use CultuurNet\UDB3\Label\ValueObjects\Privacy;
19
use CultuurNet\UDB3\Label\ValueObjects\Visibility;
20
use CultuurNet\UDB3\Offer\Commands\AbstractAddLabel;
21
use CultuurNet\UDB3\Offer\Commands\AbstractSyncLabels;
22
use ValueObjects\Identity\UUID;
23
24
class CommandHandler extends AbstractCommandHandler
25
{
26
    /**
27
     * @var RepositoryInterface
28
     */
29
    private $repository;
30
31
    /**
32
     * @var UuidGeneratorInterface
33
     */
34
    private $uuidGenerator;
35
36
    /**
37
     * CommandHandler constructor.
38
     * @param RepositoryInterface $repository
39
     * @param UuidGeneratorInterface $uuidGenerator
40
     */
41
    public function __construct(
42
        RepositoryInterface $repository,
43
        UuidGeneratorInterface $uuidGenerator
44
    ) {
45
        $this->repository = $repository;
46
        $this->uuidGenerator = $uuidGenerator;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function handle($command)
53
    {
54
        if ($command instanceof AbstractAddLabel) {
55
            $this->handleAddLabel($command);
56
        } else if ($command instanceof AbstractSyncLabels) {
57
            $this->handleSyncLabels($command);
58
        } else {
59
            parent::handle($command);
60
        }
61
    }
62
63
    /**
64
     * @param Create $create
65
     */
66
    public function handleCreate(Create $create)
67
    {
68
        $label = LabelAggregate::create(
69
            $create->getUuid(),
70
            $create->getName(),
71
            $create->getVisibility(),
72
            $create->getPrivacy()
73
        );
74
75
        $this->save($label);
76
    }
77
78
    /**
79
     * @param CreateCopy $createCopy
80
     */
81
    public function handleCreateCopy(CreateCopy $createCopy)
82
    {
83
        $label = LabelAggregate::createCopy(
84
            $createCopy->getUuid(),
85
            $createCopy->getName(),
86
            $createCopy->getVisibility(),
87
            $createCopy->getPrivacy(),
88
            $createCopy->getParentUuid()
89
        );
90
91
        $this->save($label);
92
    }
93
94
    /**
95
     * @param AbstractSyncLabels $mergeLabels
96
     */
97
    public function handleSyncLabels(AbstractSyncLabels $mergeLabels)
98
    {
99
        array_walk(
100
            $mergeLabels->getLabelCollection()->asArray(),
0 ignored issues
show
Bug introduced by
$mergeLabels->getLabelCollection()->asArray() cannot be passed to array_walk() as the parameter $array expects a reference.
Loading history...
101
            function (Label $label) {
102
                $this->createLabel($label);
103
            }
104
        );
105
    }
106
107
    /**
108
     * @param AbstractAddLabel $addLabel
109
     */
110
    public function handleAddLabel(AbstractAddLabel $addLabel)
111
    {
112
        $this->createLabel($addLabel->getLabel());
113
    }
114
115
    /**
116
     * @param MakeVisible $makeVisible
117
     */
118
    public function handleMakeVisible(MakeVisible $makeVisible)
119
    {
120
        $label = $this->load($makeVisible->getUuid());
121
122
        $label->makeVisible();
123
124
        $this->save($label);
125
    }
126
127
    /**
128
     * @param MakeInvisible $makeInvisible
129
     */
130
    public function handleMakeInvisible(MakeInvisible $makeInvisible)
131
    {
132
        $label = $this->load($makeInvisible->getUuid());
133
134
        $label->makeInvisible();
135
136
        $this->save($label);
137
    }
138
139
    /**
140
     * @param MakePublic $makePublic
141
     */
142
    public function handleMakePublic(MakePublic $makePublic)
143
    {
144
        $label = $this->load($makePublic->getUuid());
145
146
        $label->makePublic();
147
148
        $this->save($label);
149
    }
150
151
    /**
152
     * @param MakePrivate $makePrivate
153
     */
154
    public function handleMakePrivate(MakePrivate $makePrivate)
155
    {
156
        $label = $this->load($makePrivate->getUuid());
157
158
        $label->makePrivate();
159
160
        $this->save($label);
161
    }
162
163
    /**
164
     * @param UUID $uuid
165
     * @return LabelAggregate
166
     */
167
    private function load(UUID $uuid)
168
    {
169
        return $this->repository->load($uuid);
170
    }
171
172
    /**
173
     * @param LabelAggregate $label
174
     */
175
    private function save(LabelAggregate $label)
176
    {
177
        $this->repository->save($label);
178
    }
179
180
    /**
181
     * @param Label $label
182
     */
183
    private function createLabel(Label $label)
184
    {
185
        $label = LabelAggregate::create(
186
            new UUID($this->uuidGenerator->generate()),
187
            new LabelName((string)$label),
188
            Visibility::VISIBLE(),
189
            Privacy::PRIVACY_PUBLIC()
190
        );
191
        try {
192
            $this->save($label);
193
        } catch (UniqueConstraintException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
194
        }
195
    }
196
}
197