Test Failed
Push — master ( 0864dc...48ec37 )
by Petr
12:45
created

AgendaReflectFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 17
c 0
b 0
f 0
dl 0
loc 40
rs 10

2 Methods

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