AgendaDIFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 29
ccs 12
cts 12
cp 1
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 4
    public function __construct(
15
        protected readonly ContainerInterface $container,
16 4
    ) {}
17
18
    /**
19
     * {@inheritDoc}
20
     */
21 4
    public function getAgenda(string $name): AbstractAgenda
22
    {
23 4
        $className = $this->getClassName($name);
24 4
        if (!$this->container->has($className)) {
25 1
            throw new DomainException('Agenda class does not exists: ' . $name);
26
        }
27
28
        try {
29 3
            $instance = $this->container->get($className);
30 1
        } catch (ContainerExceptionInterface $ex) {
31 1
            throw new DomainException('Agenda class initialization failed: ' . $name, 0, $ex);
32
        }
33
34 2
        if ($instance instanceof AbstractAgenda) {
35 1
            return $instance;
36
        }
37
38 1
        throw new DomainException('Agenda class is not an instance of AbstractAgenda: ' . $name);
39
    }
40
}
41