Passed
Push — master ( 284873...e318bf )
by Petr
27:35
created

ParameterFactory::getByKey()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 21
ccs 12
cts 16
cp 0.75
rs 9.7666
cc 4
nc 5
nop 3
crap 4.25
1
<?php
2
3
namespace Riesenia\Pohoda\PrintRequest;
4
5
6
use DomainException;
7
use ReflectionClass;
8
use ReflectionException;
9
use Riesenia\Pohoda\ValueTransformer\SanitizeEncoding;
10
use Riesenia\Pohoda\Common;
11
12
13
class ParameterFactory
14
{
15
    /** @var array<string, class-string<Parameter>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string<Parameter>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string<Parameter>>.
Loading history...
16
    protected array $instances = [
17
        'checkbox1' => Checkbox1::class,
18
        'checkbox2' => Checkbox2::class,
19
        'checkbox3' => Checkbox3::class,
20
        'checkbox4' => Checkbox4::class,
21
        'checkbox5' => Checkbox5::class,
22
        'checkbox6' => Checkbox6::class,
23
        'checkbox7' => Checkbox7::class,
24
        'checkbox8' => Checkbox8::class,
25
        'radioButton1' => RadioButton1::class,
26
        'spin1' => Spin1::class,
27
        'currency1' => Currency1::class,
28
        'month1' => Month1::class,
29
        'month2' => Month2::class,
30
        'year1' => Year1::class,
31
        'date1' => Date1::class,
32
        'date2' => Date2::class,
33
        'date3' => Date3::class,
34
        'date4' => Date4::class,
35
        'text1' => Text1::class,
36
        'text2' => Text2::class,
37
        'text3' => Text3::class,
38
        'combobox1' => Combobox1::class,
39
        'combobox2' => Combobox2::class,
40
        'combobox3' => Combobox3::class,
41
        'comboboxEx1' => ComboboxEx1::class,
42
        'comboboxEx2' => ComboboxEx2::class,
43
    ];
44
45 3
    public function __construct(
46
        protected readonly Common\NamespacesPaths $namespacesPaths,
47
        protected readonly SanitizeEncoding $sanitizeEncoding,
48
        protected readonly string $ico,
49
    )
50
    {
51 3
    }
52
53
    /**
54
     * @param string $key
55
     * @param array<string, mixed> $data
56
     * @param bool $resolveOptions
57
     * @return Parameter
58
     */
59 3
    public function getByKey(string $key, array $data, bool $resolveOptions): Parameter
60
    {
61 3
        if (!isset($this->instances[$key])) {
62
            throw new DomainException(sprintf('The key *%s* is not known.', $key));
63
        }
64
        try {
65 3
            $reflection = new ReflectionClass($this->instances[$key]);
66 3
            $class = $reflection->newInstance(
67 3
                $this->namespacesPaths,
68 3
                $this->sanitizeEncoding,
69 3
                $data,
70 3
                $this->ico,
71 3
                $resolveOptions
72 3
            );
73
        } catch (ReflectionException $e) {
74
            throw new DomainException($e->getMessage(), $e->getCode(), $e);
75
        }
76 3
        if (!is_a($class, Parameter::class)) {
77
            throw new DomainException(sprintf('The class *%s* is not subclass of Parameter', get_class($class)));
78
        }
79 3
        return $class;
80
    }
81
82
    /**
83
     * @return string[]
84
     */
85 3
    public function getKeys(): array
86
    {
87 3
        return array_keys($this->instances);
88
    }
89
}
90