Lines 85.71% 6 / 7
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 3 / 3 100.00% 1 / 1 3
 register 100.00% 1 / 1 100.00% 1 / 1 1
 has 100.00% 1 / 1 100.00% 1 / 1 1
 get 100.00% 1 / 1 100.00% 1 / 1 1
 list 0.00% 0 / 1 0.00% 0 / 1 2
25final class StrategyRegistry
26{
27    /** @var array<string, StrategyInterface> */
28    private array $strategies = [];
29
30    /**
31     * @param array<string, StrategyInterface>|null $initial
32     */
33    public function __construct(?array $initial = null)
34    {
35        if ($initial) {
36            foreach ($initial as $action => $strategy) {
37                $this->register($action, $strategy);
38            }
39        }
40    }
41
42    public function register(string $action, StrategyInterface $strategy): void
43    {
44        $this->strategies[$action] = $strategy;
45    }
46
47    public function has(string $action): bool
48    {
49        return array_key_exists($action, $this->strategies);
50    }
51
52    public function get(string $action): ?StrategyInterface
53    {
54        return $this->strategies[$action] ?? null;
55    }
56
57    /**
58     * Liefert alle registrierten Actions.
59     * @return string[]
60     */
61    public function list(): array
62    {
63        return array_keys($this->strategies);
64    }
65}