Completed
Pull Request — develop (#188)
by A.
03:51
created

EventCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 5
A getEventNames() 0 4 1
A select() 0 15 2
A contains() 0 4 1
A getIterator() 0 4 1
1
<?php
2
3
/**
4
 * Copyright 2017 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\MiddlewareBundle\EventSourcing;
20
21
use ArrayIterator;
22
use IteratorAggregate;
23
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\InvalidArgumentException;
24
25
final class EventCollection implements IteratorAggregate
26
{
27
    /**
28
     * @var string[]
29
     */
30
    private $eventNames = [];
31
32
    public function __construct(array $eventNames)
33
    {
34
        foreach ($eventNames as $eventName) {
35
            if (!is_string($eventName) || empty($eventName)) {
36
                throw InvalidArgumentException::invalidType('non-empty string', 'eventName', $eventName);
37
            }
38
39
            if (!class_exists($eventName)) {
40
                throw new InvalidArgumentException(sprintf(
41
                    'Cannot create EventCollection: class "%s" does not exist',
42
                    $eventName
43
                ));
44
            }
45
46
            $this->eventNames[] = $eventName;
47
        }
48
    }
49
50
    /**
51
     * @return string[]
52
     */
53
    public function getEventNames()
54
    {
55
        return $this->eventNames;
56
    }
57
58
    /**
59
     * @param array $subset
60
     * @return EventCollection
61
     */
62
    public function select(array $subset)
63
    {
64
        $nonAvailableEventNames = array_diff($subset, $this->eventNames);
65
66
        if (!empty($nonAvailableEventNames)) {
67
            throw new InvalidArgumentException(
68
                sprintf(
69
                    'Subset of event names contains event names not present in collection: %s',
70
                    implode(', ', $nonAvailableEventNames)
71
                )
72
            );
73
        }
74
75
        return new self($subset);
76
    }
77
78
    /**
79
     * @param $eventName
80
     * @return bool
81
     */
82
    public function contains($eventName)
83
    {
84
        return in_array($eventName, $this->eventNames);
85
    }
86
87
    public function getIterator()
88
    {
89
        return new ArrayIterator($this->eventNames);
90
    }
91
}
92