Completed
Push — master ( 9a7a58...f5340a )
by Maxim
02:53
created

CubeManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 112
rs 10
c 0
b 0
f 0

7 Methods

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