ParameterFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 44
c 1
b 0
f 0
dl 0
loc 75
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeys() 0 3 1
A __construct() 0 6 1
A getByKey() 0 21 4
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 7
    public function __construct(
46
        protected readonly Common\NamespacesPaths $namespacesPaths,
47
        protected readonly SanitizeEncoding $sanitizeEncoding,
48
        protected readonly string $companyRegistrationNumber,
49
    )
50
    {
51 7
    }
52
53
    /**
54
     * @param string $key
55
     * @param array<string, mixed> $data
56
     * @param bool $resolveOptions
57
     * @return Parameter
58
     */
59 7
    public function getByKey(string $key, array $data, bool $resolveOptions): Parameter
60
    {
61 7
        if (!isset($this->instances[$key])) {
62 1
            throw new DomainException(sprintf('The key *%s* is not known.', $key));
63
        }
64
        try {
65 6
            $reflection = new ReflectionClass($this->instances[$key]);
66 6
            $class = $reflection->newInstance(
67 6
                $this->namespacesPaths,
68 6
                $this->sanitizeEncoding,
69 6
                $data,
70 6
                $this->companyRegistrationNumber,
71 6
                $resolveOptions
72 6
            );
73 1
        } catch (ReflectionException $e) {
74 1
            throw new DomainException($e->getMessage(), $e->getCode(), $e);
75
        }
76 5
        if (!is_a($class, Parameter::class)) {
77 1
            throw new DomainException(sprintf('The class *%s* is not subclass of Parameter', get_class($class)));
78
        }
79 4
        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