SubscriptionRegistry   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 53
ccs 10
cts 12
cp 0.8333
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addStrategy() 0 9 2
A __construct() 0 3 1
A get() 0 7 2
1
<?php
2
3
namespace Terox\SubscriptionBundle\Registry;
4
5
use Psr\Log\InvalidArgumentException;
6
use Terox\SubscriptionBundle\Exception\StrategyNotFoundException;
7
use Terox\SubscriptionBundle\Strategy\SubscriptionStrategyInterface;
8
9
/**
10
 * Subscription strategy registry.
11
 *
12
 */
13
class SubscriptionRegistry
14
{
15
    /**
16
     * @var SubscriptionStrategyInterface[]
17
     */
18
    private $strategies;
19
20
    /**
21
     * Constructor.
22
     *
23
     */
24 16
    public function __construct()
25
    {
26 16
        $this->strategies = [];
27 16
    }
28
29
    /**
30
     * Add strategy.
31
     *
32
     * @param SubscriptionStrategyInterface $strategy
33
     * @param string                        $name
34
     *
35
     * @return SubscriptionRegistry
36
     *
37
     * @throws \InvalidArgumentException
38
     */
39 16
    public function addStrategy(SubscriptionStrategyInterface $strategy, $name)
40
    {
41 16
        if (array_key_exists($name, $this->strategies)) {
42
            throw new \InvalidArgumentException(sprintf('The strategy %s is already a registered strategy.', $name));
43
        }
44
45 16
        $this->strategies[$name] = $strategy;
46
47 16
        return $this;
48
    }
49
50
    /**
51
     * Get strategy.
52
     *
53
     * @param string $name
54
     *
55
     * @return SubscriptionStrategyInterface
56
     *
57
     * @throws StrategyNotFoundException
58
     */
59 11
    public function get($name)
60
    {
61 11
        if (!array_key_exists($name, $this->strategies)) {
62
            throw new StrategyNotFoundException(sprintf('The strategy "%s" does not exist in the registry', $name));
63
        }
64
65 11
        return $this->strategies[$name];
66
    }
67
}
68