Completed
Push — master ( 82ad84...aad44c )
by Maxim
02:32
created

CubeManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace WebComplete\core\cube;
4
5
use Psr\SimpleCache\CacheInterface;
6
use WebComplete\core\utils\helpers\ClassHelper;
7
8
class CubeManager
9
{
10
11
    const FILENAME = 'Cube.php';
12
13
    /**
14
     * @var ClassHelper
15
     */
16
    protected $classHelper;
17
    /**
18
     * @var CacheInterface
19
     */
20
    protected $cache;
21
    /**
22
     * @var array
23
     */
24
    protected $registered = [];
25
26
    /**
27
     * @param ClassHelper $classHelper
28
     * @param CacheInterface $cache
29
     */
30
    public function __construct(ClassHelper $classHelper, CacheInterface $cache)
31
    {
32
        $this->classHelper = $classHelper;
33
        $this->cache = $cache;
34
    }
35
36
    /**
37
     * @param $cubeClassName
38
     *
39
     * @return AbstractCube
40
     * @throws CubeException
41
     */
42
    public function getCube($cubeClassName): AbstractCube
43
    {
44
        if (!isset($this->registered[$cubeClassName])) {
45
            throw new CubeException($cubeClassName . ' is not registered');
46
        }
47
48
        return $this->registered[$cubeClassName];
49
    }
50
51
    /**
52
     * @return AbstractCube[]
53
     */
54
    public function getCubes(): array
55
    {
56
        return $this->registered;
57
    }
58
59
    /**
60
     * @param $cubeClassName
61
     * @param array $definitions
62
     *
63
     * @throws CubeException
64
     */
65
    public function register($cubeClassName, array &$definitions)
66
    {
67
        if (!isset($this->registered[$cubeClassName])) {
68
            $cube = new $cubeClassName;
69
            if (!$cube instanceof AbstractCube) {
70
                throw new CubeException($cubeClassName . ' is not an instance of ' . AbstractCube::class);
71
            }
72
73
            $cube->registerDependencies($definitions);
74
            $this->registered[$cubeClassName] = $cube;
75
        }
76
    }
77
78
    /**
79
     * @param string $directory
80
     * @param array $definitions
81
     *
82
     * @throws \Exception
83
     */
84
    public function registerAll(string $directory, array &$definitions)
85
    {
86
        $classMap = $this->findAll($directory);
87
        foreach ($classMap as $class) {
88
            $this->register($class, $definitions);
89
        }
90
    }
91
92
    /**
93
     * @param $directory
94
     *
95
     * @return array [file => class]
96
     * @throws \Psr\SimpleCache\InvalidArgumentException
97
     * @throws \Exception
98
     * @throws \InvalidArgumentException
99
     */
100
    public function findAll($directory): array
101
    {
102
        $key = \str_replace(['::', '\\'], '.', __METHOD__) . '.' . \crc32($directory);
103
        $result = $this->cache->get($key);
104
        if (!$result) {
105
            $result = $this->classHelper->getClassMap($directory, self::FILENAME);
106
            $this->cache->set($key, $result);
107
        }
108
        return $result;
109
    }
110
}
111