Factory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initDefined() 0 11 3
A getKeyEncoder() 0 12 4
A checkObject() 0 6 2
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\KeyEncoders;
4
5
6
use kalanis\UploadPerPartes\Traits\TLangInit;
7
use kalanis\UploadPerPartes\Uploader\Config;
8
use kalanis\UploadPerPartes\UploadException;
9
use ReflectionClass;
10
use ReflectionException;
11
12
13
/**
14
 * Class Factory
15
 * @package kalanis\UploadPerPartes\Target\Local\DrivingFile\KeyEncoders
16
 * Select what will be used as data base to shared key
17
 */
18
class Factory
19
{
20
    use TLangInit;
21
22
    public const VARIANT_NAME = '1';
23
    public const VARIANT_FULL_PATH = '2';
24
    public const VARIANT_SALTED_NAME = '3';
25
    public const VARIANT_SALTED_FULL = '4';
26
    public const VARIANT_SERIALIZE = '5';
27
    public const VARIANT_JSON = '6';
28
29
    /** @var array<string|int, class-string<AEncoder>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string|int, class-string<AEncoder>> at position 6 could not be parsed: Unknown type name 'class-string' at position 6 in array<string|int, class-string<AEncoder>>.
Loading history...
30
    protected array $map = [
31
        self::VARIANT_NAME => Name::class,
32
        self::VARIANT_FULL_PATH => FullPath::class,
33
        self::VARIANT_SALTED_NAME => SaltedName::class,
34
        self::VARIANT_SALTED_FULL => SaltedFullPath::class,
35
        self::VARIANT_SERIALIZE => Serialize::class,
36
        self::VARIANT_JSON => Json::class,
37
    ];
38
39
    /**
40
     * @param Config $config
41
     * @throws UploadException
42
     * @return AEncoder
43
     */
44 27
    public function getKeyEncoder(Config $config): AEncoder
45
    {
46 27
        if (is_object($config->keyEncoder)) {
47 19
            return $this->checkObject($config->keyEncoder);
48
        }
49 8
        if (isset($this->map[$config->keyEncoder])) {
50 3
            return $this->initDefined($this->map[$config->keyEncoder]);
51
        }
52 5
        if (is_string($config->keyEncoder)) {
53 4
            return $this->initDefined($config->keyEncoder);
54
        }
55 1
        throw new UploadException($this->getUppLang()->uppKeyEncoderVariantNotSet());
56
    }
57
58
    /**
59
     * @param object $variant
60
     * @throws UploadException
61
     * @return AEncoder
62
     */
63 24
    protected function checkObject(object $variant): AEncoder
64
    {
65 24
        if ($variant instanceof AEncoder) {
66 23
            return $variant;
67
        }
68 1
        throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong(get_class($variant)));
69
    }
70
71
    /**
72
     * @param string $variant
73
     * @throws UploadException
74
     * @return AEncoder
75
     */
76 7
    protected function initDefined(string $variant): AEncoder
77
    {
78
        try {
79
            /** @var class-string<AEncoder> $variant */
80 7
            $ref = new ReflectionClass($variant);
81 6
            if ($ref->isInstantiable()) {
82 5
                return $this->checkObject($ref->newInstance($this->getUppLang()));
83
            }
84 1
            throw new UploadException($this->getUppLang()->uppKeyEncoderVariantIsWrong($variant));
85 2
        } catch (ReflectionException $ex) {
86 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
87
        }
88
    }
89
}
90