Test Failed
Pull Request — master (#19)
by Patrick
03:05
created

ActivityPluginCollection::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of forecast.it.fill project.
7
 * (c) Patrick Jaja <[email protected]>
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace ForecastAutomation\Activity\Shared\Plugin;
13
14
use ArrayAccess;
15
use Iterator;
16
17
class ActivityPluginCollection implements Iterator, ArrayAccess
18
{
19
    /**
20
     * @var \ForecastAutomation\Activity\Shared\Plugin\ActivityPluginInterface[]
21
     */
22
    private array $plugins;
23
24
    private int $position = 0;
25
26 1
    public function __construct(
27
        ActivityPluginInterface ...$plugins
28
    ) {
29 1
        $this->plugins = $plugins;
30 1
    }
31
32 1
    public function current(): ActivityPluginInterface
33
    {
34 1
        return $this->plugins[$this->position];
35
    }
36
37 1
    public function next(): void
38
    {
39 1
        ++$this->position;
40 1
    }
41
42
    public function key(): int
43
    {
44
        return $this->position;
45
    }
46
47 1
    public function valid(): bool
48
    {
49 1
        return \array_key_exists($this->position, $this->plugins);
50
    }
51
52 1
    public function rewind(): void
53
    {
54 1
        $this->position = 0;
55 1
    }
56
57
    public function offsetExists($offset): bool
58
    {
59
        return \array_key_exists($offset, $this->plugins);
60
    }
61
62
    public function offsetGet($offset): mixed
63
    {
64
        return $this->plugins[$offset] ?? null;
65
    }
66
67
    public function offsetSet($offset, $value): void
68
    {
69
        if (null === $offset) {
70
            $this->plugins[] = $value;
71
        } else {
72
            $this->plugins[$offset] = $value;
73
        }
74
    }
75
76
    public function offsetUnset($offset): void
77
    {
78
        unset($this->plugins[$offset]);
79
    }
80
81
    /**
82
     * @return \GuzzleHttp\Promise\PromiseInterface[]
83
     */
84
    public function collect(): array
85
    {
86
        $promises = [];
87
        foreach ($this->plugins as $plugin) {
88
            $promises[] = $plugin->collect();
89
        }
90
91
        return $promises;
92
    }
93
}
94