Completed
Push — release-2.x ( 370844...c278f6 )
by A.
05:18
created

EventCollection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 5
A getEventNames() 0 4 1
A formatAsEventStreamTypes() 0 9 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
    public function formatAsEventStreamTypes()
59
    {
60
        return array_map(
61
            function ($eventName) {
62
                return strtr($eventName, '\\', '.');
63
            },
64
            $this->eventNames
65
        );
66
    }
67
68
    /**
69
     * @param array $subset
70
     * @return EventCollection
71
     */
72
    public function select(array $subset)
73
    {
74
        $nonAvailableEventNames = array_diff($subset, $this->eventNames);
75
76
        if (!empty($nonAvailableEventNames)) {
77
            throw new InvalidArgumentException(
78
                sprintf(
79
                    'Subset of event names contains event names not present in collection: %s',
80
                    implode(', ', $nonAvailableEventNames)
81
                )
82
            );
83
        }
84
85
        return new self($subset);
86
    }
87
88
    /**
89
     * @param $eventName
90
     * @return bool
91
     */
92
    public function contains($eventName)
93
    {
94
        return in_array($eventName, $this->eventNames);
95
    }
96
97
    public function getIterator()
98
    {
99
        return new ArrayIterator($this->eventNames);
100
    }
101
}
102