Completed
Push — bugfix/ra-locations ( 876bb2 )
by Boy
04:43
created

RaLocationProjector::applyRaLocationRemovedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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