Completed
Pull Request — develop (#166)
by Daan van
22:27 queued 19:06
created

RaLocationProjector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
dl 0
loc 87
c 0
b 0
f 0
wmc 10
lcom 1
cbo 11
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A applyRaLocationAddedEvent() 0 12 1
A applyRaLocationRenamedEvent() 0 8 1
A applyRaLocationRelocatedEvent() 0 8 1
A applyRaLocationContactInformationChangedEvent() 0 8 1
A applyRaLocationRemovedEvent() 0 6 1
A removeRaLocationsFor() 0 4 1
A fetchRaLocationById() 0 18 3
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\ApiBundle\Configuration\Projector;
20
21
use Broadway\ReadModel\Projector;
22
use Surfnet\Stepup\Configuration\Event\InstitutionConfigurationRemovedEvent;
23
use Surfnet\Stepup\Configuration\Event\RaLocationAddedEvent;
24
use Surfnet\Stepup\Configuration\Event\RaLocationContactInformationChangedEvent;
25
use Surfnet\Stepup\Configuration\Event\RaLocationRelocatedEvent;
26
use Surfnet\Stepup\Configuration\Event\RaLocationRemovedEvent;
27
use Surfnet\Stepup\Configuration\Event\RaLocationRenamedEvent;
28
use Surfnet\Stepup\Configuration\Value\RaLocationId;
29
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation;
30
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\RaLocationRepository;
31
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
32
33
class RaLocationProjector extends Projector
34
{
35
    /**
36
     * @var RaLocationRepository
37
     */
38
    private $repository;
39
40
    public function __construct(RaLocationRepository $repository)
41
    {
42
        $this->repository = $repository;
43
    }
44
45
    public function applyRaLocationAddedEvent(RaLocationAddedEvent $event)
46
    {
47
        $raLocation = RaLocation::create(
48
            $event->raLocationId->getRaLocationId(),
49
            $event->institution,
50
            $event->raLocationName,
51
            $event->location,
52
            $event->contactInformation
53
        );
54
55
        $this->repository->save($raLocation);
56
    }
57
58
    public function applyRaLocationRenamedEvent(RaLocationRenamedEvent $event)
59
    {
60
        $raLocation = $this->fetchRaLocationById($event->raLocationId);
61
62
        $raLocation->name = $event->raLocationName;
63
64
        $this->repository->save($raLocation);
65
    }
66
67
    public function applyRaLocationRelocatedEvent(RaLocationRelocatedEvent $event)
68
    {
69
        $raLocation = $this->fetchRaLocationById($event->raLocationId);
70
71
        $raLocation->location = $event->location;
72
73
        $this->repository->save($raLocation);
74
    }
75
76
    public function applyRaLocationContactInformationChangedEvent(RaLocationContactInformationChangedEvent $event)
77
    {
78
        $raLocation = $this->fetchRaLocationById($event->raLocationId);
79
80
        $raLocation->contactInformation = $event->contactInformation;
81
82
        $this->repository->save($raLocation);
83
    }
84
85
    public function applyRaLocationRemovedEvent(RaLocationRemovedEvent $event)
86
    {
87
        $raLocation = $this->fetchRaLocationById($event->raLocationId);
88
89
        $this->repository->remove($raLocation);
90
    }
91
92
    public function removeRaLocationsFor(InstitutionConfigurationRemovedEvent $event)
93
    {
94
        $this->repository->removeRaLocationsFor($event->institution);
95
    }
96
97
    /**
98
     * @param RaLocationId $raLocationId
99
     * @return RaLocation
100
     */
101
    private function fetchRaLocationById(RaLocationId $raLocationId)
102
    {
103
        $raLocation = $this->repository->findByRaLocationId($raLocationId);
104
105
        if (is_null($raLocation)) {
106
            throw new RuntimeException(
107
                'Tried to update an RA Locations contact information, but location could not be found'
108
            );
109
        }
110
111
        if (!$raLocation instanceof RaLocation) {
112
            throw new RuntimeException(
113
                'Tried to update an RA Locations contact information, but location is of the wrong type'
114
            );
115
        }
116
117
        return $raLocation;
118
    }
119
}
120