Completed
Push — feature/add-ra-location-comman... ( af78b4...07f7b9 )
by A.
04:55
created

handleRemoveRaLocationCommand()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 26
rs 8.8571
cc 2
eloc 15
nc 2
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\RemoveRaLocationCommand;
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\InstitutionConfigurationNotFoundException;
35
36
/**
37
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Value objects
38
 */
39
class InstitutionConfigurationCommandHandler extends CommandHandler
40
{
41
    /**
42
     * @var RepositoryInterface
43
     */
44
    private $repository;
45
46
    public function __construct(RepositoryInterface $repository)
47
    {
48
        $this->repository = $repository;
49
    }
50
51
    public function handleAddRaLocationCommand(AddRaLocationCommand $command)
52
    {
53
        $institution                = new Institution($command->institution);
54
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
55
56
        try {
57
            $institutionConfiguration = $this->repository->load(
58
                $institutionConfigurationId->getInstitutionConfigurationId()
59
            );
60
        } catch (AggregateNotFoundException $exception) {
61
            $institutionConfiguration = InstitutionConfiguration::create($institutionConfigurationId, $institution);
62
        }
63
64
        $institutionConfiguration->addRaLocation(
65
            new RaLocationId($command->raLocationId),
66
            new RaLocationName($command->raLocationName),
67
            new Location($command->location),
68
            new ContactInformation($command->contactInformation)
69
        );
70
71
        $this->repository->save($institutionConfiguration);
72
    }
73
74
    public function handleChangeRaLocationCommand(ChangeRaLocationCommand $command)
75
    {
76
        $institution                = new Institution($command->institution);
77
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
78
79
        try {
80
            /** @var InstitutionConfiguration $institutionConfiguration */
81
            $institutionConfiguration = $this->repository->load(
82
                $institutionConfigurationId->getInstitutionConfigurationId()
83
            );
84
        } catch (AggregateNotFoundException $exception) {
85
            throw new InstitutionConfigurationNotFoundException(sprintf(
86
                'Cannot change RA location "%s": its InstitutionConfiguration with id "%s" not found',
87
                $command->raLocationId,
88
                $institutionConfigurationId->getInstitutionConfigurationId()
89
            ));
90
        }
91
92
        $institutionConfiguration->changeRaLocation(
93
            new RaLocationId($command->raLocationId),
94
            new RaLocationName($command->raLocationName),
95
            new Location($command->location),
96
            new ContactInformation($command->contactInformation)
97
        );
98
99
        $this->repository->save($institutionConfiguration);
100
    }
101
102
    public function handleRemoveRaLocationCommand(RemoveRaLocationCommand $command)
103
    {
104
        $institution                = new Institution($command->institution);
105
        $institutionConfigurationId = InstitutionConfigurationId::from($institution);
106
107
        try {
108
            /** @var InstitutionConfiguration $institutionConfiguration */
109
            $institutionConfiguration = $this->repository->load(
110
                $institutionConfigurationId->getInstitutionConfigurationId()
111
            );
112
        } catch (AggregateNotFoundException $exception) {
113
            throw new InstitutionConfigurationNotFoundException(
114
                sprintf(
115
                    'Cannot remove RA location "%s": its InstitutionConfiguration with id "%s" was not found',
116
                    $command->raLocationId,
117
                    $institutionConfigurationId->getInstitutionConfigurationId()
118
                )
119
            );
120
        }
121
122
        $institutionConfiguration->removeRaLocation(
123
            new RaLocationId($command->raLocationId)
124
        );
125
126
        $this->repository->save($institutionConfiguration);
127
    }
128
}
129