Completed
Push — feature/vetting-location ( 0a3f42...e920c1 )
by A.
04:06
created

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