ParameterReflectFactory::getByClassName()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace kalanis\Pohoda\DI;
4
5
use kalanis\Pohoda\PrintRequest;
6
use kalanis\PohodaException;
7
use ReflectionClass;
8
use ReflectionException;
9
10
final class ParameterReflectFactory implements ParameterFactoryInterface
11
{
12 7
    public function __construct(
13
        protected readonly DependenciesFactory $dependenciesFactory,
14 7
    ) {}
15
16
    /**
17
     * {@inheritDoc}
18
     */
19 6
    public function getByClassName(string $className): PrintRequest\Parameter
20
    {
21
        try {
22 6
            $reflection = new ReflectionClass($className);
23 6
            $class = $reflection->newInstance(
24 6
                $this->dependenciesFactory,
25 6
            );
26 1
        } catch (ReflectionException $e) {
27 1
            throw new PohodaException($e->getMessage(), $e->getCode(), $e);
28
        }
29 5
        if (!is_a($class, PrintRequest\Parameter::class)) {
30 1
            throw new PohodaException(sprintf('The class *%s* is not subclass of Parameter', get_class($class)));
31
        }
32 4
        return $class;
33
    }
34
}
35