Completed
Push — feature/create-institution-con... ( c6588d )
by A.
04:25
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2016 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\CommandHandler;
20
21
use Broadway\CommandHandling\CommandHandler;
22
use Broadway\Repository\AggregateNotFoundException;
23
use Broadway\Repository\RepositoryInterface;
24
use Surfnet\Stepup\Configuration\InstitutionConfiguration;
25
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
26
use Surfnet\Stepup\Configuration\Value\Institution;
27
use Surfnet\Stepup\Configuration\Value\Location;
28
use Surfnet\Stepup\Configuration\Value\RaLocationId;
29
use Surfnet\Stepup\Configuration\Value\RaLocationName;
30
use Surfnet\Stepup\Configuration\Value\ContactInformation;
31
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\AddRaLocationCommand;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\ChangeRaLocationCommand;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand;
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\RemoveRaLocationCommand;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\InstitutionConfigurationNotFoundException;
36
37
/**
38
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Value objects
39
 */
40
class InstitutionConfigurationCommandHandler extends CommandHandler
41
{
42
    /**
43
     * @var RepositoryInterface
44
     */
45
    private $repository;
46
47
    public function __construct(RepositoryInterface $repository)
48
    {
49
        $this->repository = $repository;
50
    }
51
52
    public function handleCreateInstitutionConfigurationCommand(CreateInstitutionConfigurationCommand $command)
53
    {
54
        $institution                = new Institution($command->institution);
55
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
56
57
        try {
58
            $institutionConfiguration = $this->repository->load(
59
                $institutionConfigurationId->getInstitutionConfigurationId()
60
            );
61
        } catch (AggregateNotFoundException $exception) {
62
            $institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution);
63
        }
64
65
        $this->repository->save($institutionConfiguration);
66
    }
67
68 View Code Duplication
    public function handleAddRaLocationCommand(AddRaLocationCommand $command)
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...
69
    {
70
        $institution                = new Institution($command->institution);
71
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
72
73
        try {
74
           /** @var InstitutionConfiguration $institutionConfiguration */
75
            $institutionConfiguration = $this->repository->load(
76
                $institutionConfigurationId->getInstitutionConfigurationId()
77
            );
78
        } catch (AggregateNotFoundException $exception) {
79
            throw new InstitutionConfigurationNotFoundException(sprintf(
80
                'Cannot add RA location "%s": its InstitutionConfiguration with id "%s" not found',
81
                $command->raLocationId,
82
                $institutionConfigurationId->getInstitutionConfigurationId()
83
            ));
84
        }
85
86
        $institutionConfiguration->addRaLocation(
87
            new RaLocationId($command->raLocationId),
88
            new RaLocationName($command->raLocationName),
89
            new Location($command->location),
90
            new ContactInformation($command->contactInformation)
91
        );
92
93
        $this->repository->save($institutionConfiguration);
94
    }
95
96 View Code Duplication
    public function handleChangeRaLocationCommand(ChangeRaLocationCommand $command)
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...
97
    {
98
        $institution                = new Institution($command->institution);
99
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
100
101
        try {
102
            /** @var InstitutionConfiguration $institutionConfiguration */
103
            $institutionConfiguration = $this->repository->load(
104
                $institutionConfigurationId->getInstitutionConfigurationId()
105
            );
106
        } catch (AggregateNotFoundException $exception) {
107
            throw new InstitutionConfigurationNotFoundException(sprintf(
108
                'Cannot change RA location "%s": its InstitutionConfiguration with id "%s" not found',
109
                $command->raLocationId,
110
                $institutionConfigurationId->getInstitutionConfigurationId()
111
            ));
112
        }
113
114
        $institutionConfiguration->changeRaLocation(
115
            new RaLocationId($command->raLocationId),
116
            new RaLocationName($command->raLocationName),
117
            new Location($command->location),
118
            new ContactInformation($command->contactInformation)
119
        );
120
121
        $this->repository->save($institutionConfiguration);
122
    }
123
124
    public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command)
125
    {
126
        $institution                = new Institution($command->institution);
127
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
128
129
        try {
130
            /** @var InstitutionConfiguration $institutionConfiguration */
131
            $institutionConfiguration = $this->repository->load(
132
                $institutionConfigurationId->getInstitutionConfigurationId()
133
            );
134
        } catch (AggregateNotFoundException $exception) {
135
            throw new InstitutionConfigurationNotFoundException(
136
                sprintf(
137
                    'Cannot remove RA location "%s": its InstitutionConfiguration with id "%s" was not found',
138
                    $command->raLocationId,
139
                    $institutionConfigurationId->getInstitutionConfigurationId()
140
                )
141
            );
142
        }
143
144
        $institutionConfiguration->removeRaLocation(
145
            new RaLocationId($command->raLocationId)
146
        );
147
148
        $this->repository->save($institutionConfiguration);
149
    }
150
}
151