AgendaFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Riesenia\Pohoda;
4
5
6
use DomainException;
7
use ReflectionClass;
8
use ReflectionException;
9
10
11
class AgendaFactory
12
{
13 22
    public function __construct(
14
        protected readonly Common\NamespacesPaths $namespacesPaths,
15
        protected readonly ValueTransformer\SanitizeEncoding $sanitizeEncoding,
16
        protected readonly string $companyNumber,
17
    )
18
    {
19 22
    }
20
21
    /**
22
     * @param string $name
23
     * @param array<string,mixed> $data
24
     * @param bool $resolveOptions
25
     * @throws DomainException
26
     * @return AbstractAgenda
27
     */
28 10
    public function getAgenda(string $name, array $data, bool $resolveOptions = true): AbstractAgenda
29
    {
30
        /** @var class-string<AbstractAgenda> $className */
31 10
        $className = __NAMESPACE__ . '\\' . $name;
32
        try {
33 10
            $reflection = new ReflectionClass($className);
34 2
        } catch (ReflectionException) {
35 2
            throw new DomainException('Agenda class does not exists: ' . $name);
36
        }
37
38 8
        if (!$reflection->isInstantiable()) {
39 2
            throw new DomainException('Agenda class cannot be initialized: ' . $name);
40
        }
41
42
        try {
43 6
            $instance = $reflection->newInstance(
44 6
                $this->namespacesPaths,
45 6
                $this->sanitizeEncoding,
46 6
                $data,
47 6
                $this->companyNumber,
48 6
                $resolveOptions,
49 6
            );
50
            // @codeCoverageIgnoreStart
51
        } catch (ReflectionException) {
52
            throw new DomainException('Agenda class initialization failed: ' . $name);
53
        }
54
        // @codeCoverageIgnoreEnd
55
56 6
        if (!is_a($instance, AbstractAgenda::class)) {
57 1
            throw new DomainException('Agenda class is not an instance of AbstractAgenda: ' . $name);
58
        }
59
60 5
        return $instance;
61
    }
62
}
63