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

ParameterDIFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getByClassName() 0 17 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\PrintRequest;
9
10
final class ParameterDIFactory implements ParameterFactoryInterface
11
{
12
    public function __construct(
13
        protected readonly ContainerInterface $container,
14
    ) {}
15
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function getByClassName(string $className): PrintRequest\Parameter
20
    {
21
        if (!$this->container->has($className)) {
22
            throw new DomainException('Parameter Entity does not exists: ' . $className);
23
        }
24
25
        try {
26
            $instance = $this->container->get($className);
27
        } catch (ContainerExceptionInterface $ex) {
28
            throw new DomainException('Parameter Entity cannot be initialized: ' . $className, 0, $ex);
29
        }
30
31
        if ($instance instanceof PrintRequest\Parameter) {
32
            return $instance;
33
        }
34
35
        throw new DomainException('Entity is not an instance of PrintRequest\Parameter: ' . $className);
36
    }
37
}
38