Passed
Push — master ( 055b76...bacc0b )
by Patrick
01:33 queued 12s
created

QueuePluginCollection::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\QueueClient\Shared\Plugin;
13
14
use ArrayAccess;
15
use Iterator;
16
17
class QueuePluginCollection implements Iterator, ArrayAccess
18
{
19
    /**
20
     * @var \ForecastAutomation\QueueClient\Shared\Plugin\QueuePluginInterface[]
21
     */
22
    public array $plugins;
23
24
    private int $position = 0;
25
26
    public function __construct(
27
        QueuePluginInterface ...$plugins
28
    ) {
29
        foreach ($plugins as $plugin) {
30
            $this->plugins[$plugin->getQueueName()] = $plugin;
31
        }
32
    }
33
34
    public function current(): QueuePluginInterface
35
    {
36
        return $this->plugins[$this->position];
37
    }
38
39
    public function next(): void
40
    {
41
        ++$this->position;
42
    }
43
44
    public function key(): int
45
    {
46
        return $this->position;
47
    }
48
49
    public function valid(): bool
50
    {
51
        return \array_key_exists($this->position, $this->plugins);
52
    }
53
54
    public function rewind(): void
55
    {
56
        $this->position = 0;
57
    }
58
59
    public function offsetExists($offset): bool
60
    {
61
        return \array_key_exists($offset, $this->plugins);
62
    }
63
64
    public function offsetGet($offset): QueuePluginInterface
65
    {
66
        if (! $this->offsetExists($offset)) {
67
            throw new \Exception(
68
                \sprintf(
69
                    'Unknown Queue %s, please register first to your configured queue dependencyprovider adapter.',
70
                    $offset
71
                )
72
            );
73
        }
74
75
        return $this->plugins[$offset];
76
    }
77
78
    public function offsetSet($offset, $value): void
79
    {
80
        if (null === $offset) {
81
            $this->plugins[] = $value;
82
        } else {
83
            $this->plugins[$offset] = $value;
84
        }
85
    }
86
87
    public function offsetUnset($offset): void
88
    {
89
        unset($this->plugins[$offset]);
90
    }
91
}
92