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

AgendaDIFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAgenda() 0 18 4
1
<?php
2
3
namespace Riesenia\Pohoda\DI;
4
5
use DomainException;
6
use Psr\Container\ContainerExceptionInterface;
7
use Psr\Container\ContainerInterface;
8
use Riesenia\Pohoda\AbstractAgenda;
9
10
final class AgendaDIFactory implements AgendaFactoryInterface
11
{
12
    use ClassNameTrait;
13
14
    public function __construct(
15
        protected readonly ContainerInterface $container,
16
    ) {}
17
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public function getAgenda(string $name): AbstractAgenda
22
    {
23
        $className = $this->getClassName($name);
24
        if (!$this->container->has($className)) {
25
            throw new DomainException('Agenda class does not exists: ' . $name);
26
        }
27
28
        try {
29
            $instance = $this->container->get($className);
30
        } catch (ContainerExceptionInterface $ex) {
31
            throw new DomainException('Agenda class initialization failed: ' . $name, 0, $ex);
32
        }
33
34
        if ($instance instanceof AbstractAgenda) {
35
            return $instance;
36
        }
37
38
        throw new DomainException('Agenda class is not an instance of AbstractAgenda: ' . $name);
39
    }
40
}
41