Factory::checkObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\Checksums;
4
5
6
use kalanis\UploadPerPartes\Traits\TLangInit;
7
use kalanis\UploadPerPartes\UploadException;
8
use kalanis\UploadPerPartes\Interfaces;
9
use ReflectionClass;
10
use ReflectionException;
11
12
13
/**
14
 * Class Factory
15
 * @package kalanis\UploadPerPartes\Target\Local\Checksums
16
 * Which checksum will be used for checking parts of content on both client and server
17
 */
18
class Factory
19
{
20
    use TLangInit;
21
22
    public const FORMAT_MD5 = 'md5';
23
    public const FORMAT_SHA1 = 'sha1';
24
25
    /** @var array<string|int, class-string<Interfaces\IChecksum>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string|int, class-...<Interfaces\IChecksum>> at position 6 could not be parsed: Unknown type name 'class-string' at position 6 in array<string|int, class-string<Interfaces\IChecksum>>.
Loading history...
26
    protected array $map = [
27
        self::FORMAT_MD5 => Md5::class,
28
        self::FORMAT_SHA1 => Sha1::class,
29
    ];
30
31
    /**
32
     * @param string $method
33
     * @throws UploadException
34
     * @return Interfaces\IChecksum
35
     */
36 7
    public function getChecksum(string $method): Interfaces\IChecksum
37
    {
38 7
        $variant = empty($method) ? self::FORMAT_MD5 : $method;
39 7
        if (isset($this->map[strval($variant)])) {
40 4
            return $this->initDefined($this->map[strval($variant)]);
41
        }
42 3
        return $this->initDefined(strval($variant));
43
    }
44
45
    /**
46
     * @param object $variant
47
     * @throws UploadException
48
     * @return Interfaces\IChecksum
49
     */
50 5
    protected function checkObject(object $variant): Interfaces\IChecksum
51
    {
52 5
        if ($variant instanceof Interfaces\IChecksum) {
53 4
            return $variant;
54
        }
55 1
        throw new UploadException($this->getUppLang()->uppChecksumVariantIsWrong(get_class($variant)));
56
    }
57
58
    /**
59
     * @param string $variant
60
     * @throws UploadException
61
     * @return Interfaces\IChecksum
62
     */
63 7
    protected function initDefined(string $variant): Interfaces\IChecksum
64
    {
65
        try {
66
            /** @var class-string<Interfaces\IChecksum> $variant */
67 7
            $ref = new ReflectionClass($variant);
68 6
            if ($ref->isInstantiable()) {
69 5
                return $this->checkObject($ref->newInstance());
70
            }
71 1
            throw new UploadException($this->getUppLang()->uppChecksumVariantIsWrong($variant));
72 3
        } catch (ReflectionException $ex) {
73 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
74
        }
75
    }
76
}
77