HistoryFactoryRegistry   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 13
dl 0
loc 44
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addHistoryFactory() 0 8 1
A __construct() 0 5 2
A getIdentifiers() 0 3 1
A getHistoryFactory() 0 7 2
1
<?php
2
3
/**
4
 * Static Analysis Results Baseliner (sarb).
5
 *
6
 * (c) Dave Liddament
7
 *
8
 * For the full copyright and licence information please view the LICENSE file distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Container;
14
15
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryFactory;
16
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryFactoryLookupService;
17
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\InvalidHistoryFactoryException;
18
use Webmozart\Assert\Assert;
19
20
final class HistoryFactoryRegistry implements HistoryFactoryLookupService
21
{
22
    /**
23
     * @var HistoryFactory[]
24
     *
25
     * @psalm-var array<string, HistoryFactory>
26
     */
27
    private $historyFactories;
28
29
    /**
30
     * HistoryFactoryRegistry constructor.
31
     *
32
     * @param HistoryFactory[] $historyFactories
33
     */
34
    public function __construct(array $historyFactories)
35
    {
36
        $this->historyFactories = [];
37
        foreach ($historyFactories as $historyFactory) {
38
            $this->addHistoryFactory($historyFactory);
39
        }
40
    }
41
42
    public function getIdentifiers(): array
43
    {
44
        return array_keys($this->historyFactories);
45
    }
46
47
    public function getHistoryFactory(string $identifier): HistoryFactory
48
    {
49
        if (!array_key_exists($identifier, $this->historyFactories)) {
50
            throw InvalidHistoryFactoryException::invalidIdentifier($identifier);
51
        }
52
53
        return $this->historyFactories[$identifier];
54
    }
55
56
    private function addHistoryFactory(HistoryFactory $historyFactory): void
57
    {
58
        $identifier = $historyFactory->getIdentifier();
59
        Assert::keyNotExists($this->historyFactories, $identifier,
60
            "Multiple History Factories configured with the identifier [$identifier]",
61
        );
62
63
        $this->historyFactories[$identifier] = $historyFactory;
64
    }
65
}
66