Completed
Push — master ( c8f3ab...1f33be )
by Jonathan
03:35 queued 47s
created

Configuration::getMemoryLimit()   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 0
crap 1
1
<?php
2
3
namespace PHPChunkit;
4
5
use InvalidArgumentException;
6
use Symfony\Component\EventDispatcher\EventDispatcher;
7
8
/**
9
 * @testClass PHPChunkit\Test\ConfigurationTest
10
 */
11
class Configuration
12
{
13
    /**
14
     * @var string
15
     */
16
    private $rootDir = '';
17
18
    /**
19
     * @var array
20
     */
21
    private $watchDirectories = [];
22
23
    /**
24
     * @var string
25
     */
26
    private $testsDirectory = '';
27
28
    /**
29
     * @var string
30
     */
31
    private $bootstrapPath = '';
32
33
    /**
34
     * @var string
35
     */
36
    private $phpunitPath = 'vendor/bin/phpunit';
37
38
    /**
39
     * @var null|EventDispatcher
40
     */
41
    private $eventDispatcher;
42
43
    /**
44
     * @var null|DatabaseSandbox
45
     */
46
    private $databaseSandbox;
47
48
    /**
49
     * @var string
50
     */
51
    private $memoryLimit = '256M';
52
53
    /**
54
     * @var int
55
     */
56
    private $numChunks = 1;
57
58 3
    public static function createFromXmlFile(string $path) : self
59
    {
60 3
        if (!file_exists($path)) {
61 1
            throw new \InvalidArgumentException(sprintf('XML file count not be found at path "%s"', $path));
62
        }
63
64 2
        $configuration = new self();
65
66 2
        $xml = simplexml_load_file($path);
67 2
        $attributes = $xml->attributes();
68
69
        $xmlMappings = [
70
            'root-dir' => [
71
                'type' => 'string',
72
                'setter' => 'setRootDir'
73 2
            ],
74
            'bootstrap' => [
75
                'type' => 'string',
76
                'setter' => 'setBootstrapPath'
77
            ],
78
            'tests-dir' => [
79
                'type' => 'string',
80
                'setter' => 'setTestsDirectory'
81
            ],
82
            'phpunit-path' => [
83
                'type' => 'string',
84
                'setter' => 'setPhpunitPath'
85
            ],
86
            'memory-limit' => [
87
                'type' => 'string',
88
                'setter' => 'setMemoryLimit'
89
            ],
90
            'num-chunks' => [
91
                'type' => 'integer',
92
                'setter' => 'setNumChunks'
93
            ],
94
            'watch-directories' => [
95
                'type' => 'array',
96
                'setter' => 'setWatchDirectories',
97
                'xmlName' => 'watch-directory',
98
            ],
99
            'database-names' => [
100
                'type' => 'array',
101
                'setter' => 'setDatabaseNames',
102
                'xmlName' => 'database-name',
103
            ],
104
        ];
105
106 2
        foreach ($xmlMappings as $name => $mapping) {
107 2
            $value = null;
108
109 2
            if ($mapping['type'] === 'array') {
110 2
                $value = (array) $xml->{$name}->{$mapping['xmlName']};
111 2
            } elseif (isset($attributes[$name])) {
112 2
                $value = $attributes[$name];
113
114 2
                settype($value, $mapping['type']);
115
            }
116
117 2
            if ($value !== null) {
118 2
                $configuration->{$mapping['setter']}($value);
119
            }
120
        }
121
122 2
        $events = (array) $xml->{'events'};
123 2
        $listeners = $events['listener'] ?? null;
124
125 2
        if ($listeners) {
126 2
            foreach ($listeners as $listener) {
127 2
                $configuration->addListener(
128 2
                    (string) $listener->attributes()['event'],
129 2
                    (string) $listener->class
130
                );
131
            }
132
        }
133
134 2
        return $configuration;
135
    }
136
137 2
    public function addListener(
138
        string $eventName,
139
        string $className,
140
        int $priority = 0) : ListenerInterface
0 ignored issues
show
Unused Code introduced by
The parameter $priority is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142 2
        $listener = new $className($this);
143
144 2
        if (!$listener instanceof ListenerInterface) {
145
            throw new InvalidArgumentException(
146
                sprintf('%s does not implement %s', $className, ListenerInterface::class)
147
            );
148
        }
149
150 2
        $this->getEventDispatcher()->addListener(
151 2
            $eventName, [$listener, 'execute']
152
        );
153
154 2
        return $listener;
155
    }
156
157 10 View Code Duplication
    public function setRootDir(string $rootDir) : self
158
    {
159 10
        if (!is_dir($rootDir)) {
160 1
            throw new \InvalidArgumentException(
161 1
                sprintf('Root directory "%s" does not exist.', $rootDir)
162
            );
163
        }
164
165 9
        $this->rootDir = realpath($rootDir);
166
167 9
        return $this;
168
    }
169
170 4
    public function getRootDir() : string
171
    {
172 4
        return $this->rootDir;
173
    }
174
175 5
    public function setWatchDirectories(array $watchDirectories) : self
176
    {
177 5
        foreach ($watchDirectories as $key => $watchDirectory) {
178 5
            if (!is_dir($watchDirectory)) {
179 1
                throw new \InvalidArgumentException(
180 1
                    sprintf('Watch directory "%s" does not exist.', $watchDirectory)
181
                );
182
            }
183
184 4
            $watchDirectories[$key] = realpath($watchDirectory);
185
        }
186
187 4
        $this->watchDirectories = $watchDirectories;
188
189 4
        return $this;
190
    }
191
192 3
    public function getWatchDirectories() : array
193
    {
194 3
        return $this->watchDirectories;
195
    }
196
197 5 View Code Duplication
    public function setTestsDirectory(string $testsDirectory) : self
198
    {
199 5
        if (!is_dir($testsDirectory)) {
200 1
            throw new \InvalidArgumentException(
201 1
                sprintf('Tests directory "%s" does not exist.', $testsDirectory)
202
            );
203
        }
204
205 4
        $this->testsDirectory = realpath($testsDirectory);
206
207 4
        return $this;
208
    }
209
210 3
    public function getTestsDirectory() : string
211
    {
212 3
        return $this->testsDirectory;
213
    }
214
215 4 View Code Duplication
    public function setBootstrapPath(string $bootstrapPath) : self
216
    {
217 4
        if (!file_exists($bootstrapPath)) {
218 1
            throw new \InvalidArgumentException(
219 1
                sprintf('Bootstrap path "%s" does not exist.', $bootstrapPath)
220
            );
221
        }
222
223 3
        $this->bootstrapPath = realpath($bootstrapPath);
224
225 3
        return $this;
226
    }
227
228 3
    public function getBootstrapPath() : string
229
    {
230 3
        return $this->bootstrapPath;
231
    }
232
233 10 View Code Duplication
    public function setPhpunitPath(string $phpunitPath) : self
234
    {
235 10
        if (!file_exists($phpunitPath)) {
236 1
            throw new \InvalidArgumentException(
237 1
                sprintf('PHPUnit path "%s" does not exist.', $phpunitPath)
238
            );
239
        }
240
241 9
        $this->phpunitPath = realpath($phpunitPath);
242
243 9
        return $this;
244
    }
245
246 3
    public function getPhpunitPath() : string
247
    {
248 3
        return $this->phpunitPath;
249
    }
250
251 1
    public function setDatabaseSandbox(DatabaseSandbox $databaseSandbox) : self
252
    {
253 1
        $this->databaseSandbox = $databaseSandbox;
254
255 1
        return $this;
256
    }
257
258 4
    public function getDatabaseSandbox() : DatabaseSandbox
259
    {
260 4
        if ($this->databaseSandbox === null) {
261 4
            $this->databaseSandbox = new DatabaseSandbox();
262
        }
263
264 4
        return $this->databaseSandbox;
265
    }
266
267 3
    public function setDatabaseNames(array $databaseNames) : self
268
    {
269 3
        $this->getDatabaseSandbox()->setDatabaseNames($databaseNames);
270
271 3
        return $this;
272
    }
273
274 1
    public function setSandboxEnabled(bool $sandboxEnabled) : self
275
    {
276 1
        $this->getDatabaseSandbox()->setSandboxEnabled($sandboxEnabled);
277
278 1
        return $this;
279
    }
280
281 2
    public function setMemoryLimit(string $memoryLimit) : self
282
    {
283 2
        $this->memoryLimit = $memoryLimit;
284
285 2
        return $this;
286
    }
287
288 2
    public function getMemoryLimit() : string
289
    {
290 2
        return $this->memoryLimit;
291
    }
292
293 2
    public function setNumChunks(int $numChunks) : self
294
    {
295 2
        $this->numChunks = $numChunks;
296
297 2
        return $this;
298
    }
299
300 1
    public function getNumChunks() : int
301
    {
302 1
        return $this->numChunks;
303
    }
304
305 1
    public function setEventDispatcher(EventDispatcher $eventDispatcher) : self
306
    {
307 1
        $this->eventDispatcher = $eventDispatcher;
308
309 1
        return $this;
310
    }
311
312 3
    public function getEventDispatcher() : EventDispatcher
313
    {
314 3
        if ($this->eventDispatcher === null) {
315 3
            $this->eventDispatcher = new EventDispatcher();
316
        }
317
318 3
        return $this->eventDispatcher;
319
    }
320
321 2
    public function throwExceptionIfConfigurationIncomplete()
322
    {
323 2
        if (!$this->rootDir) {
324 1
            throw new \InvalidArgumentException('You must configure a root directory.');
325
        }
326
327 1
        if (empty($this->watchDirectories)) {
328
            throw new \InvalidArgumentException('You must configure a watch directory.');
329
        }
330
331 1
        if (!$this->testsDirectory) {
332
            throw new \InvalidArgumentException('You must configure a tests directory.');
333
        }
334
335 1
        if (!$this->phpunitPath) {
336
            throw new \InvalidArgumentException('You must configure a phpunit path.');
337
        }
338
339 1
        return true;
340
    }
341
}
342