Test Failed
Push — master ( e493d7...b1ea68 )
by Petr
11:01
created

Factory::initDefined()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\DataEncoders;
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\DataEncoders
16
 * Drive file format - Factory to get formats
17
 *
18
 * How to pack and unpack data in info file on server
19
 */
20
class Factory
21
{
22
    use TLangInit;
23
24
    public const FORMAT_TEXT = '1';
25
    public const FORMAT_JSON = '2';
26
    public const FORMAT_LINE = '3';
27
    public const FORMAT_SERIAL = '4';
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::FORMAT_TEXT => Text::class,
32
        self::FORMAT_JSON => Json::class,
33
        self::FORMAT_LINE => Line::class,
34
        self::FORMAT_SERIAL => Serialize::class,
35
    ];
36
37
    /**
38
     * @param Config $config
39
     * @throws UploadException
40
     * @return AEncoder
41
     */
42
    public function getDataEncoder(Config $config): AEncoder
43
    {
44
        if (is_object($config->dataEncoder)) {
45
            return $this->checkObject($config->dataEncoder);
46
        }
47
        if (isset($this->map[$config->dataEncoder])) {
48
            return $this->initDefined($this->map[$config->dataEncoder]);
49
        }
50
        if (is_string($config->dataEncoder)) {
51
            return $this->initDefined($config->dataEncoder);
52
        }
53
        throw new UploadException($this->getUppLang()->uppDataEncoderVariantNotSet());
54
    }
55
56
    /**
57
     * @param object $variant
58
     * @throws UploadException
59
     * @return AEncoder
60
     */
61
    protected function checkObject(object $variant): AEncoder
62
    {
63
        if ($variant instanceof AEncoder) {
64
            return $variant;
65
        }
66
        throw new UploadException($this->getUppLang()->uppDataEncoderVariantIsWrong(get_class($variant)));
67
    }
68
69
    /**
70
     * @param string $variant
71
     * @throws UploadException
72
     * @return AEncoder
73
     */
74
    protected function initDefined(string $variant): AEncoder
75
    {
76
        try {
77
            /** @var class-string<AEncoder> $variant */
78
            $ref = new ReflectionClass($variant);
79
            if ($ref->isInstantiable()) {
80
                return $this->checkObject($ref->newInstance($this->getUppLang()));
81
            }
82
            throw new UploadException($this->getUppLang()->uppDataEncoderVariantIsWrong($variant));
83
        } catch (ReflectionException $ex) {
84
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
85
        }
86
    }
87
}
88