Factory   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 42
c 1
b 0
f 0
dl 0
loc 83
ccs 30
cts 30
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getStorage() 0 31 11
A fromPairs() 0 15 5
1
<?php
2
3
namespace kalanis\kw_storage\Storage\Target;
4
5
6
use kalanis\kw_storage\Interfaces;
7
use kalanis\kw_storage\StorageException;
8
use kalanis\kw_storage\Traits\TLang;
9
use ReflectionException;
10
11
12
/**
13
 * Class Factory
14
 * @package kalanis\kw_storage\Storage\Target
15
 * Simple example of storage factory
16
 */
17
class Factory
18
{
19
    use TLang;
20
21
    /** @var array<string, class-string<Interfaces\Target\ITarget>|null> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-stri...s\Target\ITarget>|null> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string<Interfaces\Target\ITarget>|null>.
Loading history...
22
    protected static array $pairs = [
23
        'mem' => Memory::class,
24
        'memory' => Memory::class,
25
        'vol' => Volume::class,
26
        'volume' => Volume::class,
27
        'volume::flat' => VolumeTargetFlat::class,
28
        'local' => Volume::class,
29
        'local::flat' => VolumeTargetFlat::class,
30
        'drive' => Volume::class,
31
        'drive::flat' => VolumeTargetFlat::class,
32
        'none' => null,
33
    ];
34
35 44
    public function __construct(?Interfaces\IStTranslations $stLang = null)
36
    {
37 44
        $this->setStLang($stLang);
38
    }
39
40
    /**
41
     * @param object|array<string, string|object>|string|null $params
42
     * @throws StorageException
43
     * @return Interfaces\Target\ITarget|null storage adapter or empty for no storage set
44
     */
45 31
    public function getStorage($params): ?Interfaces\Target\ITarget
46
    {
47 31
        if ($params instanceof Interfaces\Target\ITarget) {
48 2
            return $params;
49
        }
50
51
        try {
52 30
            if (is_array($params)) {
53 14
                if (isset($params['storage'])) {
54 13
                    if (is_object($params['storage'])) {
55 2
                        if ($params['storage'] instanceof Interfaces\Target\ITarget) {
56 1
                            return $params['storage'];
57
                        } else {
58 1
                            return null;
59
                        }
60
                    }
61 11
                    $lang = (isset($params['lang']) && is_object($params['lang']) && ($params['lang'] instanceof Interfaces\IStTranslations))
62 1
                        ? $params['lang']
63 11
                        : $this->getStLang();
64 11
                    return $this->fromPairs(strval($params['storage']), $lang);
65
                }
66
            }
67
68 17
            if (is_string($params)) {
69 14
                return $this->fromPairs(strval($params), $this->getStLang());
70
            }
71
72 3
            return null;
73
74 1
        } catch (ReflectionException $ex) {
75 1
            throw new StorageException($ex->getMessage(), $ex->getCode(), $ex);
76
        }
77
    }
78
79
    /**
80
     * @param string $name
81
     * @param Interfaces\IStTranslations|null $lang
82
     * @throws ReflectionException
83
     * @return Interfaces\Target\ITarget|null
84
     */
85 25
    protected function fromPairs(string $name, ?Interfaces\IStTranslations $lang = null): ?Interfaces\Target\ITarget
86
    {
87 25
        if (isset(static::$pairs[$name])) {
88 21
            $class = static::$pairs[$name];
89 21
            if (is_string($class)) {
90 21
                $reflection = new \ReflectionClass($class);
91 20
                if ($reflection->isInstantiable()) {
92 20
                    $obj = $reflection->newInstance($lang);
93 20
                    if ($obj instanceof Interfaces\Target\ITarget) {
94 20
                        return $obj;
95
                    }
96
                }
97
            }
98
        }
99 4
        return null;
100
    }
101
}
102