AgendaReflectFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 17
c 0
b 0
f 0
dl 0
loc 40
ccs 15
cts 15
cp 1
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 11
    public function __construct(
15
        protected readonly DependenciesFactory $dependenciesFactory,
16 11
    ) {}
17
18
    /**
19
     * {@inheritDoc}
20
     */
21 11
    public function getAgenda(string $name): AbstractAgenda
22
    {
23
        /** @var class-string<AbstractAgenda> $className */
24 11
        $className = $this->getClassName($name);
25
        try {
26 11
            $reflection = new ReflectionClass($className);
27 2
        } catch (ReflectionException) {
28 2
            throw new DomainException('Agenda class does not exists: ' . $name);
29
        }
30
31 9
        if (!$reflection->isInstantiable()) {
32 2
            throw new DomainException('Agenda class cannot be initialized: ' . $name);
33
        }
34
35
        try {
36 7
            $instance = $reflection->newInstance(
37 7
                $this->dependenciesFactory,
38 7
            );
39
            // @codeCoverageIgnoreStart
40
        } catch (ReflectionException) {
41
            throw new DomainException('Agenda class initialization failed: ' . $name);
42
        }
43
        // @codeCoverageIgnoreEnd
44
45 7
        if (!is_a($instance, AbstractAgenda::class)) {
46 1
            throw new DomainException('Agenda class is not an instance of AbstractAgenda: ' . $name);
47
        }
48
49 6
        return $instance;
50
    }
51
}
52