AgendaFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 44
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAgenda() 0 31 5
1
<?php
2
3
namespace Riesenia\Pohoda;
4
5
use DomainException;
6
use ReflectionClass;
7
use ReflectionException;
8
9
class AgendaFactory
10
{
11 23
    public function __construct(
12
        protected readonly Common\NamespacesPaths $namespacesPaths,
13
        protected readonly ValueTransformer\SanitizeEncoding $sanitizeEncoding,
14
        protected Common\OptionsResolver\Normalizers\NormalizerFactory $normalizerFactory = new Common\OptionsResolver\Normalizers\NormalizerFactory(),
15 23
    ) {}
16
17
    /**
18
     * @param string $name
19
     * @throws DomainException
20
     * @return AbstractAgenda
21
     */
22 11
    public function getAgenda(string $name): AbstractAgenda
23
    {
24
        /** @var class-string<AbstractAgenda> $className */
25 11
        $className = __NAMESPACE__ . '\\' . $name;
26
        try {
27 11
            $reflection = new ReflectionClass($className);
28 2
        } catch (ReflectionException) {
29 2
            throw new DomainException('Agenda class does not exists: ' . $name);
30
        }
31
32 9
        if (!$reflection->isInstantiable()) {
33 2
            throw new DomainException('Agenda class cannot be initialized: ' . $name);
34
        }
35
36
        try {
37 7
            $instance = $reflection->newInstance(
38 7
                $this->namespacesPaths,
39 7
                $this->sanitizeEncoding,
40 7
                $this->normalizerFactory,
41 7
            );
42
            // @codeCoverageIgnoreStart
43
        } catch (ReflectionException) {
44
            throw new DomainException('Agenda class initialization failed: ' . $name);
45
        }
46
        // @codeCoverageIgnoreEnd
47
48 7
        if (!is_a($instance, AbstractAgenda::class)) {
49 1
            throw new DomainException('Agenda class is not an instance of AbstractAgenda: ' . $name);
50
        }
51
52 6
        return $instance;
53
    }
54
}
55