LangFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 52
ccs 19
cts 19
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLang() 0 7 2
A __construct() 0 3 1
A checkObject() 0 6 2
A initDefined() 0 11 3
1
<?php
2
3
namespace kalanis\UploadPerPartes\Uploader;
4
5
6
use kalanis\UploadPerPartes\UploadException;
7
use kalanis\UploadPerPartes\Interfaces;
8
use ReflectionClass;
9
use ReflectionException;
10
11
12
/**
13
 * Class LangFactory
14
 * @package kalanis\UploadPerPartes\Uploader
15
 * Pass selected language in either object or class name
16
 */
17
class LangFactory
18
{
19
    protected string $uppLangVariantWrong = '';
20
21 23
    public function __construct(string $uppLangVariantWrong = 'Lang variant *%s* is wrong!')
22
    {
23 23
        $this->uppLangVariantWrong = $uppLangVariantWrong;
24 23
    }
25
26
    /**
27
     * @param Config $config
28
     * @throws UploadException
29
     * @return Interfaces\IUppTranslations
30
     */
31 23
    public function getLang(Config $config): Interfaces\IUppTranslations
32
    {
33 23
        $variant = $config->lang ?? new Translations();
34 23
        if (is_object($variant)) {
35 20
            return $this->checkObject($variant);
36
        }
37 3
        return $this->initDefined($variant);
38
    }
39
40
    /**
41
     * @param object $variant
42
     * @throws UploadException
43
     * @return Interfaces\IUppTranslations
44
     */
45 21
    protected function checkObject(object $variant): Interfaces\IUppTranslations
46
    {
47 21
        if ($variant instanceof Interfaces\IUppTranslations) {
48 19
            return $variant;
49
        }
50 2
        throw new UploadException(sprintf($this->uppLangVariantWrong, get_class($variant)));
51
    }
52
53
    /**
54
     * @param string $variant
55
     * @throws UploadException
56
     * @return Interfaces\IUppTranslations
57
     */
58 3
    protected function initDefined(string $variant): Interfaces\IUppTranslations
59
    {
60
        try {
61
            /** @var class-string<Interfaces\IUppTranslations> $variant */
62 3
            $ref = new ReflectionClass($variant);
63 2
            if ($ref->isInstantiable()) {
64 1
                return $this->checkObject($ref->newInstance());
65
            }
66 1
            throw new UploadException(sprintf($this->uppLangVariantWrong, $variant));
67 3
        } catch (ReflectionException $ex) {
68 1
            throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
69
        }
70
    }
71
}
72