Factory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkObject() 0 6 2
A initDefined() 0 11 3
A getDataModifier() 0 12 4
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\DataModifiers;
4
5
6
use kalanis\UploadPerPartes\Traits\TLangInit;
7
use kalanis\UploadPerPartes\UploadException;
8
use ReflectionClass;
9
use ReflectionException;
10
11
12
/**
13
 * Class EncodeFactory
14
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\DataModifiers
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_BASE64 = 2;
23
    public const VARIANT_HEX = 3;
24
25
    /** @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...
26
    protected array $map = [
27
        self::VARIANT_CLEAR => Clear::class,
28
        self::VARIANT_BASE64 => Base64::class,
29
        self::VARIANT_HEX => Hex::class,
30
    ];
31
32
    /**
33
     * @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...
34
     * @throws UploadException
35
     * @return AModifier
36
     */
37 27
    public function getDataModifier($variant): AModifier
38
    {
39 27
        if (is_object($variant)) {
40 19
            return $this->checkObject($variant);
41
        }
42 8
        if (isset($this->map[strval($variant)])) {
43 3
            return $this->initDefined($this->map[strval($variant)]);
44
        }
45 5
        if (is_string($variant)) {
46 4
            return $this->initDefined($variant);
47
        }
48 1
        throw new UploadException($this->getUppLang()->uppDataModifierVariantNotSet());
49
    }
50
51
    /**
52
     * @param object $variant
53
     * @throws UploadException
54
     * @return AModifier
55
     */
56 24
    protected function checkObject(object $variant): AModifier
57
    {
58 24
        if ($variant instanceof AModifier) {
59 23
            return $variant;
60
        }
61 1
        throw new UploadException($this->getUppLang()->uppDataModifierVariantIsWrong(get_class($variant)));
62
    }
63
64
    /**
65
     * @param string $variant
66
     * @throws UploadException
67
     * @return AModifier
68
     */
69 7
    protected function initDefined(string $variant): AModifier
70
    {
71
        try {
72
            /** @var class-string<AModifier> $variant */
73 7
            $ref = new ReflectionClass($variant);
74 6
            if ($ref->isInstantiable()) {
75 5
                return $this->checkObject($ref->newInstance($this->getUppLang()));
76
            }
77 1
            throw new UploadException($this->getUppLang()->uppDataModifierVariantIsWrong($variant));
78 2
        } catch (ReflectionException $ex) {
79 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
80
        }
81
    }
82
}
83