Completed
Push — master ( 0a12e7...77ea51 )
by Jonathan
02:43
created

Configuration::setBootstrapPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PHPChunkit;
6
7
use InvalidArgumentException;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
10
/**
11
 * @testClass PHPChunkit\Test\ConfigurationTest
12
 */
13
class Configuration
14
{
15
    /**
16
     * @var string
17
     */
18
    private $rootDir = '';
19
20
    /**
21
     * @var array
22
     */
23
    private $watchDirectories = [];
24
25
    /**
26
     * @var string
27
     */
28
    private $testsDirectory = '';
29
30
    /**
31
     * @var string
32
     */
33
    private $bootstrapPath = '';
34
35
    /**
36
     * @var string
37
     */
38
    private $phpunitPath = 'vendor/bin/phpunit';
39
40
    /**
41
     * @var null|EventDispatcher
42
     */
43
    private $eventDispatcher;
44
45
    /**
46
     * @var null|DatabaseSandbox
47
     */
48
    private $databaseSandbox;
49
50
    /**
51
     * @var string
52
     */
53
    private $memoryLimit = '256M';
54
55
    /**
56
     * @var int
57
     */
58
    private $numChunks = 1;
59
60 2
    public static function createFromXmlFile(string $path) : self
61
    {
62 2
        if (!file_exists($path)) {
63 1
            throw new \InvalidArgumentException(sprintf('XML file count not be found at path "%s"', $path));
64
        }
65
66 1
        $configuration = new self();
67
68 1
        $xml = simplexml_load_file($path);
69 1
        $attributes = $xml->attributes();
70
71
        $xmlMappings = [
72
            'root-dir' => [
73
                'type' => 'string',
74
                'setter' => 'setRootDir'
75 1
            ],
76
            'bootstrap' => [
77
                'type' => 'string',
78
                'setter' => 'setBootstrapPath'
79
            ],
80
            'tests-dir' => [
81
                'type' => 'string',
82
                'setter' => 'setTestsDirectory'
83
            ],
84
            'phpunit-path' => [
85
                'type' => 'string',
86
                'setter' => 'setPhpunitPath'
87
            ],
88
            'memory-limit' => [
89
                'type' => 'string',
90
                'setter' => 'setMemoryLimit'
91
            ],
92
            'num-chunks' => [
93
                'type' => 'integer',
94
                'setter' => 'setNumChunks'
95
            ],
96
            'watch-directories' => [
97
                'type' => 'array',
98
                'setter' => 'setWatchDirectories',
99
                'xmlName' => 'watch-directory',
100
            ],
101
            'database-names' => [
102
                'type' => 'array',
103
                'setter' => 'setDatabaseNames',
104
                'xmlName' => 'database-name',
105
            ],
106
        ];
107
108 1
        foreach ($xmlMappings as $name => $mapping) {
109 1
            $value = null;
110
111 1
            if ($mapping['type'] === 'array') {
112 1
                $value = (array) $xml->{$name}->{$mapping['xmlName']};
113 1
            } elseif (isset($attributes[$name])) {
114 1
                $value = $attributes[$name];
115
116 1
                settype($value, $mapping['type']);
117
            }
118
119 1
            if ($value !== null) {
120 1
                $configuration->{$mapping['setter']}($value);
121
            }
122
        }
123
124 1
        $events = (array) $xml->{'events'};
125 1
        $listeners = $events['listener'] ?? null;
126
127 1
        if ($listeners) {
128 1
            foreach ($listeners as $listener) {
129 1
                $configuration->addListener(
130 1
                    (string) $listener->attributes()['event'],
131 1
                    (string) $listener->class
132
                );
133
            }
134
        }
135
136 1
        return $configuration;
137
    }
138
139 1
    public function addListener(
140
        string $eventName,
141
        string $className,
142
        int $priority = 0) : ListenerInterface
143
    {
144 1
        $listener = new $className($this);
145
146 1
        if (!$listener instanceof ListenerInterface) {
147
            throw new InvalidArgumentException(
148
                sprintf('%s does not implement %s', $className, ListenerInterface::class)
149
            );
150
        }
151
152 1
        $this->getEventDispatcher()->addListener(
153 1
            $eventName, [$listener, 'execute'], $priority
154
        );
155
156 1
        return $listener;
157
    }
158
159 9
    public function setRootDir(string $rootDir) : self
160
    {
161 9
        return $this->setPath('rootDir', $rootDir);
162
    }
163
164 3
    public function getRootDir() : string
165
    {
166 3
        return $this->rootDir;
167
    }
168
169 4
    public function setWatchDirectories(array $watchDirectories) : self
170
    {
171 4
        foreach ($watchDirectories as $key => $watchDirectory) {
172 4
            if (!is_dir($watchDirectory)) {
173 1
                throw new \InvalidArgumentException(
174 1
                    sprintf('Watch directory "%s" does not exist.', $watchDirectory)
175
                );
176
            }
177
178 3
            $watchDirectories[$key] = realpath($watchDirectory);
179
        }
180
181 3
        $this->watchDirectories = $watchDirectories;
182
183 3
        return $this;
184
    }
185
186 3
    public function getWatchDirectories() : array
187
    {
188 3
        return $this->watchDirectories;
189
    }
190
191 4
    public function setTestsDirectory(string $testsDirectory) : self
192
    {
193 4
        return $this->setPath('testsDirectory', $testsDirectory);
194
    }
195
196 2
    public function getTestsDirectory() : string
197
    {
198 2
        return $this->testsDirectory;
199
    }
200
201 3
    public function setBootstrapPath(string $bootstrapPath) : self
202
    {
203 3
        return $this->setPath('bootstrapPath', $bootstrapPath);
204
    }
205
206 2
    public function getBootstrapPath() : string
207
    {
208 2
        return $this->bootstrapPath;
209
    }
210
211 9
    public function setPhpunitPath(string $phpunitPath) : self
212
    {
213 9
        return $this->setPath('phpunitPath', $phpunitPath);
214
    }
215
216 3
    public function getPhpunitPath() : string
217
    {
218 3
        return $this->phpunitPath;
219
    }
220
221 1
    public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self
222
    {
223 1
        $this->databaseSandbox = $databaseSandbox;
224
225 1
        return $this;
226
    }
227
228 3
    public function getDatabaseSandbox() : DatabaseSandbox
229
    {
230 3
        if ($this->databaseSandbox === null) {
231 3
            $this->databaseSandbox = new DatabaseSandbox();
232
        }
233
234 3
        return $this->databaseSandbox;
235
    }
236
237 2
    public function setDatabaseNames(array $databaseNames) : self
238
    {
239 2
        $this->getDatabaseSandbox()->setDatabaseNames($databaseNames);
240
241 2
        return $this;
242
    }
243
244
    public function setSandboxEnabled(bool $sandboxEnabled) : self
245
    {
246
        $this->getDatabaseSandbox()->setSandboxEnabled($sandboxEnabled);
247
248
        return $this;
249
    }
250
251 1
    public function setMemoryLimit(string $memoryLimit) : self
252
    {
253 1
        $this->memoryLimit = $memoryLimit;
254
255 1
        return $this;
256
    }
257
258 2
    public function getMemoryLimit() : string
259
    {
260 2
        return $this->memoryLimit;
261
    }
262
263 1
    public function setNumChunks(int $numChunks) : self
264
    {
265 1
        $this->numChunks = $numChunks;
266
267 1
        return $this;
268
    }
269
270 1
    public function getNumChunks() : int
271
    {
272 1
        return $this->numChunks;
273
    }
274
275 1
    public function setEventDispatcher(EventDispatcher $eventDispatcher) : self
276
    {
277 1
        $this->eventDispatcher = $eventDispatcher;
278
279 1
        return $this;
280
    }
281
282 2
    public function getEventDispatcher() : EventDispatcher
283
    {
284 2
        if ($this->eventDispatcher === null) {
285 2
            $this->eventDispatcher = new EventDispatcher();
286
        }
287
288 2
        return $this->eventDispatcher;
289
    }
290
291
    public function isSetup()
292
    {
293
        if (!$this->rootDir) {
294
            return false;
295
        }
296
297
        if (!$this->testsDirectory) {
298
            return false;
299
        }
300
301
        if (!$this->phpunitPath) {
302
            return false;
303
        }
304
305
        return true;
306
    }
307
308 16
    private function setPath(string $name, string $path) : self
309
    {
310 16
        if (!file_exists($path)) {
311 4
            throw new \InvalidArgumentException(
312 4
                sprintf('%s "%s" does not exist.', $name, $path)
313
            );
314
        }
315
316 12
        $this->$name = realpath($path);
317
318 12
        return $this;
319
    }
320
}
321