EventCollection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A select() 0 14 2
A contains() 0 3 1
A getIterator() 0 3 1
A getEventNames() 0 3 1
A formatAsEventStreamTypes() 0 5 1
A __construct() 0 17 6
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing;
20
21
use ArrayIterator;
22
use Iterator;
23
use IteratorAggregate;
24
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\InvalidArgumentException;
25
26
/**
27
 * @implements IteratorAggregate<int, string>
28
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
29
final class EventCollection implements IteratorAggregate
30
{
31
    /**
32
     * @var string[]
33
     */
34
    private array $eventNames = [];
35
36
    /**
37
     * @param string[] $eventNames
38
     */
39
    public function __construct(array $eventNames)
40
    {
41
        foreach ($eventNames as $eventName) {
42
            if (!is_string($eventName) || ($eventName === '' || $eventName === '0')) {
43
                throw InvalidArgumentException::invalidType('non-empty string', 'eventName', $eventName);
44
            }
45
46
            if (!class_exists($eventName)) {
47
                throw new InvalidArgumentException(
48
                    sprintf(
49
                        'Cannot create EventCollection: class "%s" does not exist',
50
                        $eventName,
51
                    ),
52
                );
53
            }
54
55
            $this->eventNames[] = $eventName;
56
        }
57
    }
58
59
    /**
60
     * @return string[]
61
     */
62
    public function getEventNames(): array
63
    {
64
        return $this->eventNames;
65
    }
66
67
    /**
68
     * @return string[]
69
     */
70
    public function formatAsEventStreamTypes(): array
71
    {
72
        return array_map(
73
            fn($eventName): string => strtr($eventName, '\\', '.'),
74
            $this->eventNames,
75
        );
76
    }
77
78
    public function select(array $subset): self
79
    {
80
        $nonAvailableEventNames = array_diff($subset, $this->eventNames);
81
82
        if ($nonAvailableEventNames !== []) {
83
            throw new InvalidArgumentException(
84
                sprintf(
85
                    'Subset of event names contains event names not present in collection: %s',
86
                    implode(', ', $nonAvailableEventNames),
87
                ),
88
            );
89
        }
90
91
        return new self($subset);
92
    }
93
94
95
    public function contains(string $eventName): bool
96
    {
97
        return in_array($eventName, $this->eventNames);
98
    }
99
100
    /**
101
     * @return Iterator<int, string>
102
     */
103
    public function getIterator(): Iterator
104
    {
105
        return new ArrayIterator($this->eventNames);
106
    }
107
}
108