PluginFactoryManager   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
c 1
b 0
f 0
dl 0
loc 42
ccs 15
cts 16
cp 0.9375
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactory() 0 9 2
A add() 0 3 1
A all() 0 3 1
A __construct() 0 4 2
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\HTTPlugModule;
6
7
use InvalidArgumentException;
8
use TMV\HTTPlugModule\PluginFactory\PluginFactory;
9
10
use function array_key_exists;
11
12
class PluginFactoryManager
13
{
14
    /** @var array<string, PluginFactory> */
15
    private array $factories;
16
17
    /**
18
     * @param array<string, PluginFactory> $factories
19 3
     */
20
    public function __construct(array $factories = [])
21 3
    {
22 3
        foreach ($factories as $alias => $factory) {
23
            $this->add($alias, $factory);
24 3
        }
25
    }
26 3
27
    public function add(string $alias, PluginFactory $factory): void
28 3
    {
29 3
        $this->factories[$alias] = $factory;
30
    }
31 1
32
    public function has(string $alias): bool
33 1
    {
34
        return array_key_exists($alias, $this->factories);
35
    }
36
37
    /**
38
     * @return array<string, PluginFactory>
39 1
     */
40
    public function all(): array
41 1
    {
42
        return $this->factories;
43
    }
44 1
45
    public function getFactory(string $alias): PluginFactory
46 1
    {
47
        $factory = $this->factories[$alias] ?? null;
48 1
49
        if (! $factory) {
50
            throw new InvalidArgumentException('Unable to find a plugin factory for alias ' . $alias);
51
        }
52 1
53
        return $factory;
54
    }
55
}
56