Completed
Push — master ( b3f90f...c5536c )
by Daniel
03:02
created

Commander   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 19
c 6
b 1
f 2
lcom 1
cbo 10
dl 0
loc 221
ccs 56
cts 56
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A initialize() 0 17 3
A getCache() 0 10 2
A getMappingDriver() 0 10 2
A getEntityManager() 0 14 3
A getSchemaTool() 0 10 2
A getSchemaValidator() 0 10 2
A getLogger() 0 10 2
A getTaskManager() 0 8 2
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander;
9
10
use Doctrine\Common\Cache\Cache;
11
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Doctrine\ORM\Tools\SchemaTool;
14
use Doctrine\ORM\Tools\SchemaValidator;
15
use GravityMedia\Commander\Commander\TaskManager;
16
use GravityMedia\Commander\Provider\CacheProvider;
17
use GravityMedia\Commander\Provider\EntityManagerProvider;
18
use GravityMedia\Commander\Provider\LoggerProvider;
19
use GravityMedia\Commander\Provider\MappingDriverProvider;
20
use GravityMedia\Commander\Provider\SchemaToolProvider;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Commander class.
25
 *
26
 * @package GravityMedia\Commander
27
 */
28
class Commander
29
{
30
    /**
31
     * The entity namespace.
32
     */
33
    const ENTITY_NAMESPACE = 'GravityMedia\Commander\ORM';
34
35
    /**
36
     * The logger name.
37
     */
38
    const LOGGER_NAME = 'COMMANDER';
39
40
    /**
41
     * @var bool
42
     */
43
    protected $initialized;
44
45
    /**
46
     * @var Config
47
     */
48
    protected $config;
49
50
    /**
51
     * The cache.
52
     *
53
     * @var Cache
54
     */
55
    protected $cache;
56
57
    /**
58
     * The mapping driver.
59
     *
60
     * @var MappingDriver
61
     */
62
    protected $mappingDriver;
63
64
    /**
65
     * The entity manager.
66
     *
67
     * @var EntityManagerInterface
68
     */
69
    protected $entityManager;
70
71
    /**
72
     * The schema tool.
73
     *
74
     * @var SchemaTool
75
     */
76
    protected $schemaTool;
77
78
    /**
79
     * The schema validator.
80
     *
81
     * @var SchemaValidator
82
     */
83
    protected $schemaValidator;
84
85
    /**
86
     * The logger.
87
     *
88
     * @var LoggerInterface
89
     */
90
    protected $logger;
91
92
    /**
93
     * The task manager.
94
     *
95
     * @var TaskManager
96
     */
97
    protected $taskManager;
98
99
    /**
100
     * Create commander object.
101
     *
102
     * @param Config $config
103
     */
104 12
    public function __construct(Config $config)
105
    {
106 12
        $this->initialized = false;
107 12
        $this->config = $config;
108 12
    }
109
110
    /**
111
     * Initialize commander.
112
     *
113
     * @return $this
114
     */
115 8
    public function initialize()
116
    {
117 8
        if ($this->initialized) {
118 2
            return $this;
119
        }
120
121 8
        $this->initialized = true;
122
123 8
        if ($this->getSchemaValidator()->schemaInSyncWithMetadata()) {
124 6
            return $this;
125
        }
126
127 2
        $classes = $this->getEntityManager()->getMetadataFactory()->getAllMetadata();
128 2
        $this->getSchemaTool()->updateSchema($classes);
129
130 2
        return $this;
131
    }
132
133
    /**
134
     * Get cache.
135
     *
136
     * @return Cache
137
     */
138 8
    public function getCache()
139
    {
140 8
        if (null === $this->cache) {
141 8
            $provider = new CacheProvider($this->config);
142
143 8
            $this->cache = $provider->getCache();
144 4
        }
145
146 8
        return $this->cache;
147
    }
148
149
    /**
150
     * Get mapping driver.
151
     *
152
     * @return MappingDriver
153
     */
154 6
    public function getMappingDriver()
155
    {
156 6
        if (null === $this->mappingDriver) {
157 6
            $provider = new MappingDriverProvider($this->getCache());
158
159 6
            $this->mappingDriver = $provider->getMappingDriver();
160 3
        }
161
162 6
        return $this->mappingDriver;
163
    }
164
165
    /**
166
     * Get entity manager.
167
     *
168
     * @return EntityManagerInterface
169
     *
170
     * @throws \LogicException
171
     */
172 6
    public function getEntityManager()
173
    {
174 6
        if (!$this->initialized) {
175 2
            throw new \LogicException('The entity manager is not available before initialization');
176
        }
177
178 4
        if (null === $this->entityManager) {
179 4
            $provider = new EntityManagerProvider($this->config, $this->getCache(), $this->getMappingDriver());
180
181 4
            $this->entityManager = $provider->getEntityManager();
182 2
        }
183
184 4
        return $this->entityManager;
185
    }
186
187
    /**
188
     * Get schema tool.
189
     *
190
     * @return SchemaTool
191
     */
192 2
    public function getSchemaTool()
193
    {
194 2
        if (null === $this->schemaTool) {
195 2
            $provider = new SchemaToolProvider($this->getEntityManager());
196
197 2
            $this->schemaTool = $provider->getSchemaTool();
198 1
        }
199
200 2
        return $this->schemaTool;
201
    }
202
203
    /**
204
     * Get schema validator.
205
     *
206
     * @return SchemaValidator
207
     */
208 2
    public function getSchemaValidator()
209
    {
210 2
        if (null === $this->schemaValidator) {
211 2
            $provider = new SchemaToolProvider($this->getEntityManager());
212
213 2
            $this->schemaValidator = $provider->getSchemaValidator();
214 1
        }
215
216 2
        return $this->schemaValidator;
217
    }
218
219
    /**
220
     * Get logger.
221
     *
222
     * @return LoggerInterface
223
     */
224 2
    public function getLogger()
225
    {
226 2
        if (null === $this->logger) {
227 2
            $provider = new LoggerProvider($this->config);
228
229 2
            $this->logger = $provider->getLogger();
230 1
        }
231
232 2
        return $this->logger;
233
    }
234
235
    /**
236
     * Get task manager.
237
     *
238
     * @return TaskManager
239
     */
240 2
    public function getTaskManager()
241
    {
242 2
        if (null === $this->taskManager) {
243 2
            $this->taskManager = new TaskManager($this->getEntityManager());
244 1
        }
245
246 2
        return $this->taskManager;
247
    }
248
}
249