Completed
Pull Request — master (#238)
by Luc
09:35 queued 04:39
created

CommandHandler   B

Complexity

Total Complexity 16

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 17
dl 0
loc 175
rs 7.8571

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 10 3
A handleCreate() 0 11 1
A handleCreateCopy() 0 12 1
A handleAddLabel() 0 4 1
A handleMakeVisible() 0 8 1
A handleMakeInvisible() 0 8 1
A handleMakePublic() 0 8 1
A handleMakePrivate() 0 8 1
A load() 0 4 1
A save() 0 4 1
A createLabel() 0 13 2
A handleSyncLabels() 0 11 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
        $labelsArray = $mergeLabels->getLabelCollection()->asArray();
100
101
        array_walk(
102
            $labelsArray,
103
            function (Label $label) {
104
                $this->createLabel($label);
105
            }
106
        );
107
    }
108
109
    /**
110
     * @param AbstractAddLabel $addLabel
111
     */
112
    public function handleAddLabel(AbstractAddLabel $addLabel)
113
    {
114
        $this->createLabel($addLabel->getLabel());
115
    }
116
117
    /**
118
     * @param MakeVisible $makeVisible
119
     */
120
    public function handleMakeVisible(MakeVisible $makeVisible)
121
    {
122
        $label = $this->load($makeVisible->getUuid());
123
124
        $label->makeVisible();
125
126
        $this->save($label);
127
    }
128
129
    /**
130
     * @param MakeInvisible $makeInvisible
131
     */
132
    public function handleMakeInvisible(MakeInvisible $makeInvisible)
133
    {
134
        $label = $this->load($makeInvisible->getUuid());
135
136
        $label->makeInvisible();
137
138
        $this->save($label);
139
    }
140
141
    /**
142
     * @param MakePublic $makePublic
143
     */
144
    public function handleMakePublic(MakePublic $makePublic)
145
    {
146
        $label = $this->load($makePublic->getUuid());
147
148
        $label->makePublic();
149
150
        $this->save($label);
151
    }
152
153
    /**
154
     * @param MakePrivate $makePrivate
155
     */
156
    public function handleMakePrivate(MakePrivate $makePrivate)
157
    {
158
        $label = $this->load($makePrivate->getUuid());
159
160
        $label->makePrivate();
161
162
        $this->save($label);
163
    }
164
165
    /**
166
     * @param UUID $uuid
167
     * @return LabelAggregate
168
     */
169
    private function load(UUID $uuid)
170
    {
171
        return $this->repository->load($uuid);
172
    }
173
174
    /**
175
     * @param LabelAggregate $label
176
     */
177
    private function save(LabelAggregate $label)
178
    {
179
        $this->repository->save($label);
180
    }
181
182
    /**
183
     * @param Label $label
184
     */
185
    private function createLabel(Label $label)
186
    {
187
        $label = LabelAggregate::create(
188
            new UUID($this->uuidGenerator->generate()),
189
            new LabelName((string)$label),
190
            Visibility::VISIBLE(),
191
            Privacy::PRIVACY_PUBLIC()
192
        );
193
        try {
194
            $this->save($label);
195
        } catch (UniqueConstraintException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
196
        }
197
    }
198
}
199