Factory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 67
ccs 19
cts 19
cp 1
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkObject() 0 6 2
A getKeyModifier() 0 12 4
A initDefined() 0 11 3
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\KeyModifiers;
4
5
6
use kalanis\UploadPerPartes\Traits\TLangInit;
7
use kalanis\UploadPerPartes\UploadException;
8
use ReflectionClass;
9
use ReflectionException;
10
11
12
/**
13
 * Class Factory
14
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\KeyModifiers
15
 * Select correct type of pack form for passing data there and back
16
 */
17
class Factory
18
{
19
    use TLangInit;
20
21
    public const VARIANT_CLEAR = 1;
22
    public const VARIANT_RANDOM = 2;
23
    public const VARIANT_BASE64 = 3;
24
    public const VARIANT_MD5 = 4;
25
    public const VARIANT_HEX = 5;
26
    public const VARIANT_SUFFIX = 6;
27
28
    /** @var array<string|int, class-string<AModifier>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string|int, class-string<AModifier>> at position 6 could not be parsed: Unknown type name 'class-string' at position 6 in array<string|int, class-string<AModifier>>.
Loading history...
29
    protected array $map = [
30
        self::VARIANT_CLEAR => Clear::class,
31
        self::VARIANT_BASE64 => Base64::class,
32
        self::VARIANT_HEX => Hex::class,
33
        self::VARIANT_SUFFIX => Suffix::class,
34
    ];
35
36
    /**
37
     * @param int|class-string<AModifier>|object|string|null $variant
0 ignored issues
show
Documentation Bug introduced by
The doc comment int|class-string<AModifier>|object|string|null at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in int|class-string<AModifier>|object|string|null.
Loading history...
38
     * @throws UploadException
39
     * @return AModifier
40
     */
41 27
    public function getKeyModifier($variant): AModifier
42
    {
43 27
        if (is_object($variant)) {
44 19
            return $this->checkObject($variant);
45
        }
46 8
        if (isset($this->map[strval($variant)])) {
47 3
            return $this->initDefined($this->map[strval($variant)]);
48
        }
49 5
        if (is_string($variant)) {
50 4
            return $this->initDefined($variant);
51
        }
52 1
        throw new UploadException($this->getUppLang()->uppKeyModifierVariantNotSet());
53
    }
54
55
    /**
56
     * @param object $variant
57
     * @throws UploadException
58
     * @return AModifier
59
     */
60 24
    protected function checkObject(object $variant): AModifier
61
    {
62 24
        if ($variant instanceof AModifier) {
63 23
            return $variant;
64
        }
65 1
        throw new UploadException($this->getUppLang()->uppKeyModifierVariantIsWrong(get_class($variant)));
66
    }
67
68
    /**
69
     * @param string $variant
70
     * @throws UploadException
71
     * @return AModifier
72
     */
73 7
    protected function initDefined(string $variant): AModifier
74
    {
75
        try {
76
            /** @var class-string<AModifier> $variant */
77 7
            $ref = new ReflectionClass($variant);
78 6
            if ($ref->isInstantiable()) {
79 5
                return $this->checkObject($ref->newInstance($this->getUppLang()));
80
            }
81 1
            throw new UploadException($this->getUppLang()->uppKeyModifierVariantIsWrong($variant));
82 2
        } catch (ReflectionException $ex) {
83 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
84
        }
85
    }
86
}
87