StrategyRegistry::getName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 2
nc 2
nop 1
crap 6
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
}