ParameterFactory::getKeys()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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