StrategyRegistry   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 54
rs 10
c 0
b 0
f 0
ccs 0
cts 23
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassByName() 0 8 2
A getName() 0 10 2
A getMap() 0 4 1
A setMap() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Infrastructure\Strategy;
6
7
8
/**
9
 * Class StrategyRegistry
10
 *
11
 * @package Webhook\Domain\Infrastructure\Strategy
12
 */
13
final class StrategyRegistry
14
{
15
    private static $map = [
16
        'exponential' => ExponentialStrategy::class,
17
        'linear'      => LinearStrategy::class,
18
    ];
19
20
    /**
21
     * @param string $name
22
     *
23
     * @return string
24
     */
25
    public static function getClassByName(string $name):string
26
    {
27
        if (!array_key_exists($name, self::$map)) {
28
            throw new \InvalidArgumentException('Unsupported strategy class.');
29
        }
30
31
        return self::$map[$name];
32
    }
33
34
    /**
35
     * @param StrategyInterface $strategy
36
     *
37
     * @return string
38
     */
39
    public static function getName(StrategyInterface $strategy): string
40
    {
41
        $class = get_class($strategy);
42
43
        if (!in_array($class, self::$map)) {
44
            throw new \InvalidArgumentException('Unsupported strategy.');
45
        }
46
47
        return array_flip(self::$map)[$class];
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public static function getMap(): array
54
    {
55
        return self::$map;
56
    }
57
58
    /**
59
     * @param array $map
60
     */
61
    public static function setMap(array $map)
62
    {
63
        self::$map = $map;
64
    }
65
66
}