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

StorageInjectorTrait::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
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