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

ParameterReflectFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getByClassName() 0 14 3
1
<?php
2
3
namespace Riesenia\Pohoda\DI;
4
5
use DomainException;
6
use ReflectionClass;
7
use ReflectionException;
8
use Riesenia\Pohoda\PrintRequest;
9
10
final class ParameterReflectFactory implements ParameterFactoryInterface
11
{
12
    public function __construct(
13
        protected readonly DependenciesFactory $dependenciesFactory,
14
    ) {}
15
16
    /**
17
     * {@inheritDoc}
18
     */
19
    public function getByClassName(string $className): PrintRequest\Parameter
20
    {
21
        try {
22
            $reflection = new ReflectionClass($className);
23
            $class = $reflection->newInstance(
24
                $this->dependenciesFactory,
25
            );
26
        } catch (ReflectionException $e) {
27
            throw new DomainException($e->getMessage(), $e->getCode(), $e);
28
        }
29
        if (!is_a($class, PrintRequest\Parameter::class)) {
30
            throw new DomainException(sprintf('The class *%s* is not subclass of Parameter', get_class($class)));
31
        }
32
        return $class;
33
    }
34
}
35