Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

Whitelist::applyWhitelistCreatedEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 5
rs 9.4285
c 2
b 0
f 1
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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\Stepup\Identity;
20
21
use Broadway\EventSourcing\EventSourcedAggregateRoot;
22
use Surfnet\Stepup\Exception\DomainException;
23
use Surfnet\Stepup\Identity\Api\Whitelist as WhitelistApi;
24
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
25
use Surfnet\Stepup\Identity\Event\InstitutionsAddedToWhitelistEvent;
26
use Surfnet\Stepup\Identity\Event\InstitutionsRemovedFromWhitelistEvent;
27
use Surfnet\Stepup\Identity\Event\WhitelistCreatedEvent;
28
use Surfnet\Stepup\Identity\Event\WhitelistReplacedEvent;
29
30
final class Whitelist extends EventSourcedAggregateRoot implements WhitelistApi
31
{
32
    /**
33
     * There can ever be only one whitelist, so using a fixed UUIDv4
34
     */
35
    const WHITELIST_AGGREGATE_ID = '125ccee5-d650-437a-a0b0-6bf17c8188fa';
36
37
    /**
38
     * @var InstitutionCollection The collection of institutions currently on the whitelist
39
     */
40
    private $whitelist;
41
42
    public function __construct()
43
    {
44
    }
45
46
    public function getAggregateRootId()
47
    {
48
        return self::WHITELIST_AGGREGATE_ID;
49
    }
50
51
    public static function create(InstitutionCollection $institutionCollection)
52
    {
53
        $whitelist = new self();
54
        $whitelist->apply(new WhitelistCreatedEvent($institutionCollection));
55
56
        return $whitelist;
57
    }
58
59
    public function replaceAll(InstitutionCollection $institutionCollection)
60
    {
61
        $this->apply(new WhitelistReplacedEvent($institutionCollection));
62
    }
63
64 View Code Duplication
    public function add(InstitutionCollection $institutionCollection)
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...
65
    {
66
        foreach ($institutionCollection as $institution) {
67
            if ($this->whitelist->contains($institution)) {
68
                throw new DomainException(sprintf(
69
                    'Cannot add institution "%s" as it is already whitelisted',
70
                    $institution
71
                ));
72
            }
73
        }
74
75
        $this->apply(new InstitutionsAddedToWhitelistEvent($institutionCollection));
76
    }
77
78 View Code Duplication
    public function remove(InstitutionCollection $institutionCollection)
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...
79
    {
80
        foreach ($institutionCollection as $institution) {
81
            if (!$this->whitelist->contains($institution)) {
82
                throw new DomainException(sprintf(
83
                    'Cannot remove institution "%s" as it is not whitelisted',
84
                    $institution
85
                ));
86
            }
87
        }
88
89
        $this->apply(new InstitutionsRemovedFromWhitelistEvent($institutionCollection));
90
    }
91
92
    protected function applyWhitelistCreatedEvent(WhitelistCreatedEvent $event)
93
    {
94
        $this->whitelist = new InstitutionCollection();
95
        $this->whitelist->addAllFrom($event->whitelistedInstitutions);
96
    }
97
98
    protected function applyWhitelistReplacedEvent(WhitelistReplacedEvent $event)
99
    {
100
        $this->whitelist = new InstitutionCollection();
101
        $this->whitelist->addAllFrom($event->whitelistedInstitutions);
102
    }
103
104
    protected function applyInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event)
105
    {
106
        $this->whitelist->addAllFrom($event->addedInstitutions);
107
    }
108
109
    protected function applyInstitutionsRemovedFromWhitelistEvent(InstitutionsRemovedFromWhitelistEvent $event)
110
    {
111
        $this->whitelist->removeAllIn($event->removedInstitutions);
112
    }
113
}
114