Passed
Push — master ( ccc9fa...bee35f )
by Gabor
03:14
created

StorageInjectorTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 40
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addStorageInstances() 0 13 3
A __call() 0 8 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Traits;
15
16
use WebHemi\Data\StorageInterface;
17
use WebHemi\Data\Storage;
18
19
/**
20
 * Trait StorageTrait
21
 */
22
trait StorageInjectorTrait
23
{
24
    /** @var StorageInterface[] */
25
    protected $dataStorages = [];
26
27
    /**
28
     * Add Storage instances to internal library.
29
     *
30
     * @param StorageInterface[] ...$dataStorages
31
     */
32
    protected function addStorageInstances(array $dataStorages) : void
33
    {
34
        foreach ($dataStorages as $instance) {
35
            if ($instance instanceof StorageInterface) {
36
                $storageClassFullName = get_class($instance);
37
                $classNamespacePath = explode('\\', $storageClassFullName);
38
39
                $storageClass = array_pop($classNamespacePath);
40
41
                $this->dataStorages[$storageClass] = $instance;
42
            }
43
        }
44
    }
45
46
    /**
47
     * Retrieves
48
     *
49
     * @param string $name
50
     * @param array $arguments
51
     * @return null|StorageInterface
52
     */
53
    public function __call(string $name, array $arguments) : ? StorageInterface
54
    {
55
        unset($arguments);
56
57
        $className = preg_replace('/^get/', '', $name);
58
59
        return $this->dataStorages[$className] ?? null;
60
    }
61
}
62