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

ParameterDIFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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