Completed
Pull Request — master (#244)
by Luc
04:17
created

OrganizerCommandHandler::createLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Organizer;
4
5
use Broadway\CommandHandling\CommandHandlerInterface;
6
use Broadway\Repository\RepositoryInterface;
7
use CultuurNet\UDB3\Label;
8
use CultuurNet\UDB3\Label\ValueObjects\Visibility;
9
use CultuurNet\UDB3\Organizer\Commands\AbstractLabelCommand;
10
use CultuurNet\UDB3\Organizer\Commands\AddLabel;
11
use CultuurNet\UDB3\Organizer\Commands\DeleteOrganizer;
12
use CultuurNet\UDB3\Organizer\Commands\RemoveLabel;
13
use ValueObjects\String\String as StringLiteral;
14
use CultuurNet\UDB3\Label\ReadModels\JSON\Repository\ReadRepositoryInterface;
15
16
class OrganizerCommandHandler implements CommandHandlerInterface
17
{
18
    /**
19
     * @var RepositoryInterface
20
     */
21
    private $organizerRepository;
22
23
    /**
24
     * @var ReadRepositoryInterface
25
     */
26
    private $labelRepository;
27
28
    /**
29
     * @var OrganizerRelationServiceInterface[]
30
     */
31
    private $organizerRelationServices;
32
33
    /**
34
     * @param RepositoryInterface $organizerRepository
35
     * @param ReadRepositoryInterface $labelRepository
36
     */
37
    public function __construct(
38
        RepositoryInterface $organizerRepository,
39
        ReadRepositoryInterface $labelRepository
40
    ) {
41
        $this->organizerRepository = $organizerRepository;
42
        $this->labelRepository = $labelRepository;
43
        $this->organizerRelationServices = [];
44
    }
45
46
    /**
47
     * @param OrganizerRelationServiceInterface $relationService
48
     * @return OrganizerCommandHandler
49
     */
50
    public function withOrganizerRelationService(OrganizerRelationServiceInterface $relationService)
51
    {
52
        $c = clone $this;
53
        $c->organizerRelationServices[] = $relationService;
54
        return $c;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    protected function getCommandHandlerMethods()
61
    {
62
        return [
63
            DeleteOrganizer::class => 'deleteOrganizer',
64
            AddLabel::class => 'addLabel',
65
            RemoveLabel::class => 'removeLabel'
66
        ];
67
    }
68
69
    /**
70
     * @param mixed $command
71
     */
72
    public function handle($command)
73
    {
74
        $class = get_class($command);
75
        $handlers = $this->getCommandHandlerMethods();
76
77
        if (isset($handlers[$class])) {
78
            $method = $handlers[$class];
79
            $this->{$method}($command);
80
        }
81
    }
82
83
    /**
84
     * @param AddLabel $addLabel
85
     */
86
    protected function addLabel(AddLabel $addLabel)
87
    {
88
        $organizer = $this->loadOrganizer($addLabel->getOrganizerId());
89
90
        $organizer->addLabel($this->createLabel($addLabel));
91
92
        $this->organizerRepository->save($organizer);
93
    }
94
95
    /**
96
     * @param RemoveLabel $removeLabel
97
     */
98
    protected function removeLabel(RemoveLabel $removeLabel)
99
    {
100
        $organizer = $this->loadOrganizer($removeLabel->getOrganizerId());
101
102
        $organizer->removeLabel($this->createLabel($removeLabel));
103
104
        $this->organizerRepository->save($organizer);
105
    }
106
107
    /**
108
     * @param AbstractLabelCommand $labelCommand
109
     * @return Label
110
     */
111 View Code Duplication
    private function createLabel(AbstractLabelCommand $labelCommand)
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...
112
    {
113
        $labelName = new StringLiteral((string) $labelCommand->getLabel());
114
        $label = $this->labelRepository->getByName($labelName);
115
116
        return new Label(
117
            $labelName->toNative(),
118
            $label->getVisibility() === Visibility::VISIBLE()
119
        );
120
    }
121
122
    /**
123
     * @param DeleteOrganizer $deleteOrganizer
124
     */
125
    public function deleteOrganizer(DeleteOrganizer $deleteOrganizer)
126
    {
127
        $id = $deleteOrganizer->getOrganizerId();
128
129
        // First remove all relations to the given organizer.
130
        foreach ($this->organizerRelationServices as $relationService) {
131
            $relationService->deleteOrganizer($id);
132
        }
133
134
        // Delete the organizer itself.
135
        $organizer = $this->loadOrganizer($id);
136
137
        $organizer->delete();
138
139
        $this->organizerRepository->save($organizer);
140
    }
141
142
    /**
143
     * Makes it easier to type hint to Organizer.
144
     *
145
     * @param string $id
146
     * @return Organizer
147
     */
148
    protected function loadOrganizer($id)
149
    {
150
        return $this->organizerRepository->load($id);
151
    }
152
}
153