Completed
Pull Request — develop (#188)
by A.
07:09 queued 03:37
created

EventCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 5
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
     * @param array $subset
52
     * @return EventCollection
53
     */
54
    public function select(array $subset)
55
    {
56
        $nonAvailableEventNames = array_diff($subset, $this->eventNames);
57
58
        if (!empty($nonAvailableEventNames)) {
59
            throw new InvalidArgumentException(
60
                sprintf(
61
                    'Subset of event names contains event names not present in collection: %s',
62
                    implode(', ', $nonAvailableEventNames)
63
                )
64
            );
65
        }
66
67
        return new self($subset);
68
    }
69
70
    /**
71
     * @param $eventName
72
     * @return bool
73
     */
74
    public function contains($eventName)
75
    {
76
        return in_array($eventName, $this->eventNames);
77
    }
78
79
    public function getIterator()
80
    {
81
        return new ArrayIterator($this->eventNames);
82
    }
83
}
84