ProjectorCollection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A selectByNames() 0 18 3
A add() 0 3 1
A getProjectorNames() 0 5 1
A contains() 0 3 1
A getIterator() 0 3 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 Broadway\EventHandling\EventListener as ProjectorInterface;
23
use Iterator;
24
use IteratorAggregate;
25
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\InvalidArgumentException;
26
27
/**
28
 * @implements IteratorAggregate<ProjectorInterface>
29
 */
30
final class ProjectorCollection implements IteratorAggregate
31
{
32
    /**
33
     * @var ProjectorInterface[]
34
     */
35
    private array $projectors = [];
36
37
    public function add(ProjectorInterface $projector): void
38
    {
39
        $this->projectors[$projector::class] = $projector;
40
    }
41
42
    /**
43
     * @return string[]
44
     */
45
    public function getProjectorNames(): array
46
    {
47
        return array_map(
48
            fn(ProjectorInterface $projector): string => $projector::class,
49
            array_values($this->projectors),
50
        );
51
    }
52
53
    /**
54
     * @return ProjectorCollection
55
     */
56
    public function selectByNames(array $projectorNames): ProjectorCollection
57
    {
58
        $subsetCollection = new ProjectorCollection;
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
59
60
        foreach ($projectorNames as $projectorName) {
61
            if (!array_key_exists($projectorName, $this->projectors)) {
62
                throw new InvalidArgumentException(
63
                    sprintf(
64
                        'Cannot select a subset of projectors, because projector "%s" is not present in the collection',
65
                        $projectorName,
66
                    ),
67
                );
68
            }
69
70
            $subsetCollection->add($this->projectors[$projectorName]);
71
        }
72
73
        return $subsetCollection;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function contains(ProjectorInterface $projector): bool
80
    {
81
        return array_key_exists($projector::class, $this->projectors);
82
    }
83
84
    public function getIterator(): Iterator
85
    {
86
        return new ArrayIterator($this->projectors);
87
    }
88
}
89