Configuration::addListener()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 3
crap 2
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 3
    public static function createFromXmlFile(string $path) : self
61
    {
62 3
        if (!file_exists($path)) {
63 1
            throw new \InvalidArgumentException(sprintf('XML file count not be found at path "%s"', $path));
64
        }
65
66 2
        $configuration = new self();
67
68 2
        $xml = simplexml_load_file($path);
69 2
        $attributes = $xml->attributes();
70
71
        $xmlMappings = [
72
            'root-dir' => [
73
                'type' => 'string',
74
                'setter' => 'setRootDir'
75 2
            ],
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 2
        foreach ($xmlMappings as $name => $mapping) {
109 2
            $value = null;
110
111 2
            if ($mapping['type'] === 'array') {
112 2
                $value = (array) $xml->{$name}->{$mapping['xmlName']};
113 2
            } elseif (isset($attributes[$name])) {
114 2
                $value = $attributes[$name];
115
116 2
                settype($value, $mapping['type']);
117
            }
118
119 2
            if ($value !== null) {
120 2
                $configuration->{$mapping['setter']}($value);
121
            }
122
        }
123
124 2
        $events = (array) $xml->{'events'};
125 2
        $listeners = $events['listener'] ?? null;
126
127 2
        if ($listeners) {
128 2
            foreach ($listeners as $listener) {
129 2
                $configuration->addListener(
130 2
                    (string) $listener->attributes()['event'],
131 2
                    (string) $listener->class
132
                );
133
            }
134
        }
135
136 2
        return $configuration;
137
    }
138
139 3
    public function addListener(
140
        string $eventName,
141
        string $className,
142
        int $priority = 0) : ListenerInterface
143
    {
144 3
        $listener = new $className($this);
145
146 3
        if (!$listener instanceof ListenerInterface) {
147 1
            throw new InvalidArgumentException(
148 1
                sprintf('%s does not implement %s', $className, ListenerInterface::class)
149
            );
150
        }
151
152 2
        $this->getEventDispatcher()->addListener(
153 2
            $eventName, [$listener, 'execute'], $priority
154
        );
155
156 2
        return $listener;
157
    }
158
159 11
    public function setRootDir(string $rootDir) : self
160
    {
161 11
        return $this->setPath('rootDir', $rootDir);
162
    }
163
164 4
    public function getRootDir() : string
165
    {
166 4
        return $this->rootDir;
167
    }
168
169 5
    public function setWatchDirectories(array $watchDirectories) : self
170
    {
171 5
        foreach ($watchDirectories as $key => $watchDirectory) {
172 5
            if (!is_dir($watchDirectory)) {
173 1
                throw new \InvalidArgumentException(
174 1
                    sprintf('Watch directory "%s" does not exist.', $watchDirectory)
175
                );
176
            }
177
178 4
            $watchDirectories[$key] = realpath($watchDirectory);
179
        }
180
181 4
        $this->watchDirectories = $watchDirectories;
182
183 4
        return $this;
184
    }
185
186 4
    public function getWatchDirectories() : array
187
    {
188 4
        return $this->watchDirectories;
189
    }
190
191 6
    public function setTestsDirectory(string $testsDirectory) : self
192
    {
193 6
        return $this->setPath('testsDirectory', $testsDirectory);
194
    }
195
196 3
    public function getTestsDirectory() : string
197
    {
198 3
        return $this->testsDirectory;
199
    }
200
201 4
    public function setBootstrapPath(string $bootstrapPath) : self
202
    {
203 4
        return $this->setPath('bootstrapPath', $bootstrapPath);
204
    }
205
206 3
    public function getBootstrapPath() : string
207
    {
208 3
        return $this->bootstrapPath;
209
    }
210
211 10
    public function setPhpunitPath(string $phpunitPath) : self
212
    {
213 10
        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 5
    public function getDatabaseSandbox() : DatabaseSandbox
229
    {
230 5
        if ($this->databaseSandbox === null) {
231 5
            $this->databaseSandbox = new DatabaseSandbox();
232
        }
233
234 5
        return $this->databaseSandbox;
235
    }
236
237 3
    public function setDatabaseNames(array $databaseNames) : self
238
    {
239 3
        $this->getDatabaseSandbox()->setDatabaseNames($databaseNames);
240
241 3
        return $this;
242
    }
243
244 2
    public function setSandboxEnabled(bool $sandboxEnabled) : self
245
    {
246 2
        $this->getDatabaseSandbox()->setSandboxEnabled($sandboxEnabled);
247
248 2
        return $this;
249
    }
250
251 2
    public function setMemoryLimit(string $memoryLimit) : self
252
    {
253 2
        $this->memoryLimit = $memoryLimit;
254
255 2
        return $this;
256
    }
257
258 2
    public function getMemoryLimit() : string
259
    {
260 2
        return $this->memoryLimit;
261
    }
262
263 2
    public function setNumChunks(int $numChunks) : self
264
    {
265 2
        $this->numChunks = $numChunks;
266
267 2
        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 3
    public function getEventDispatcher() : EventDispatcher
283
    {
284 3
        if ($this->eventDispatcher === null) {
285 3
            $this->eventDispatcher = new EventDispatcher();
286
        }
287
288 3
        return $this->eventDispatcher;
289
    }
290
291 2
    public function isSetup()
292
    {
293 2
        $setup = true;
294
295 2
        if (!$this->rootDir) {
296 1
            $setup = false;
297
        }
298
299 2
        if (!$this->testsDirectory) {
300 1
            $setup = false;
301
        }
302
303 2
        return $setup;
304
    }
305
306 18
    private function setPath(string $name, string $path) : self
307
    {
308 18
        if (!file_exists($path)) {
309 4
            throw new \InvalidArgumentException(
310 4
                sprintf('%s "%s" does not exist.', $name, $path)
311
            );
312
        }
313
314 14
        $this->$name = realpath($path);
315
316 14
        return $this;
317
    }
318
}
319