Completed
Push — master ( 48076b...47aa10 )
by Kevin
03:28
created

SubscriberRegistry::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Zenstruck\Queue;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
final class SubscriberRegistry
9
{
10
    private $subscribers = [];
11
12
    /**
13
     * @param array|Subscriber[] $subscribers
14
     */
15 5
    public function __construct(array $subscribers = [])
16
    {
17 5
        foreach ($subscribers as $name => $subscriber) {
18 5
            $this->add($name, $subscriber);
19 5
        }
20 5
    }
21
22
    /**
23
     * @param string     $name
24
     * @param Subscriber $subscriber
25
     */
26 5
    public function add($name, Subscriber $subscriber)
27
    {
28 5
        $this->subscribers[$name] = $subscriber;
29 5
    }
30
31
    /**
32
     * @return array|Subscriber[]
33
     */
34 1
    public function all()
35
    {
36 1
        return $this->subscribers;
37
    }
38
39
    /**
40
     * @param string $name
41
     *
42
     * @return Subscriber
43
     *
44
     * @throws \InvalidArgumentException
45
     */
46 4
    public function get($name)
47
    {
48 4
        if (!isset($this->subscribers[$name])) {
49
            throw new \InvalidArgumentException(sprintf('Subscriber "%s" not found.', $name));
50
        }
51
52 4
        return $this->subscribers[$name];
53
    }
54
}
55