Passed
Pull Request — master (#9)
by John
02:59
created

DandelionServiceProvider::createValidateCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion;
6
7
use Dandelion\Configuration\ConfigurationFinder;
8
use Dandelion\Configuration\ConfigurationLoader;
9
use Dandelion\Configuration\ConfigurationValidator;
10
use Dandelion\Console\Command\InitializeCommand;
11
use Dandelion\Console\Command\ReleaseAllCommand;
12
use Dandelion\Console\Command\ReleaseCommand;
13
use Dandelion\Console\Command\SplitAllCommand;
14
use Dandelion\Console\Command\SplitCommand;
15
use Dandelion\Console\Command\ValidateCommand;
16
use Dandelion\Filesystem\Filesystem;
17
use Dandelion\Operation\Initializer;
18
use Dandelion\Operation\Releaser;
19
use Dandelion\Operation\Result\MessageFactory;
20
use Dandelion\Operation\ResultFactory;
21
use Dandelion\Operation\Splitter;
22
use Dandelion\Process\ProcessFactory;
23
use Dandelion\Process\ProcessPoolFactory;
24
use Dandelion\VersionControl\Git;
25
use Dandelion\VersionControl\SplitshLite;
26
use Pimple\Container;
27
use Pimple\ServiceProviderInterface;
28
use Swaggest\JsonSchema\Schema;
29
use Symfony\Component\Finder\Finder;
30
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
31
use Symfony\Component\Serializer\Encoder\JsonEncoder;
32
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
33
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
34
use Symfony\Component\Serializer\Serializer;
35
36
use function rtrim;
37
use function sprintf;
38
use function str_repeat;
39
40
class DandelionServiceProvider implements ServiceProviderInterface
41
{
42
    /**
43
     * @param \Pimple\Container $container
44
     */
45
    public function register(Container $container): void
46
    {
47
        $container = $this->registerDirectoryPaths($container);
48
        $container = $this->registerFilesystem($container);
49
        $container = $this->registerFinder($container);
50
        $container = $this->registerSerializer($container);
51
        $container = $this->registerConfigurationLoader($container);
52
        $container = $this->registerConfigurationFinder($container);
53
        $container = $this->registerProcessFactory($container);
54
        $container = $this->registerProcessPoolFactory($container);
55
        $container = $this->registerResultFactory($container);
56
        $container = $this->registerMessageFactory($container);
57
        $container = $this->registerGit($container);
58
        $container = $this->registerSplitshLite($container);
59
        $container = $this->registerSplitter($container);
60
        $container = $this->registerReleaser($container);
61
        $container = $this->registerConfigurationValidator($container);
62
        $container = $this->registerInitializer($container);
63
        $this->registerCommands($container);
64
    }
65
66
    /**
67
     * @param \Pimple\Container $container
68
     *
69
     * @return \Pimple\Container
70
     */
71
    protected function registerDirectoryPaths(Container $container): Container
72
    {
73
        $rootDir = rtrim(__DIR__, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR
74
            . str_repeat('..' . DIRECTORY_SEPARATOR, 2);
75
76
        $container->offsetSet('root_dir', static function () use ($rootDir) {
77
            return $rootDir;
78
        });
79
80
        $container->offsetSet('src_dir', static function () use ($rootDir) {
81
            return sprintf('%ssrc%s', $rootDir, DIRECTORY_SEPARATOR);
82
        });
83
84
        $container->offsetSet('resources_dir', static function () use ($rootDir) {
85
            return sprintf('%sresources%s', $rootDir, DIRECTORY_SEPARATOR);
86
        });
87
88
        $container->offsetSet('bin_dir', static function () use ($rootDir) {
89
            return sprintf('%sbin%s', $rootDir, DIRECTORY_SEPARATOR);
90
        });
91
92
        return $container;
93
    }
94
95
    /**
96
     * @param \Pimple\Container $container
97
     *
98
     * @return \Pimple\Container
99
     */
100
    protected function registerCommands(Container $container): Container
101
    {
102
        $self = $this;
103
104
        $container->offsetSet('commands', static function (Container $container) use ($self) {
105
            return [
106
                $self->createSplitCommand($container),
107
                $self->createSplitAllCommand($container),
108
                $self->createReleaseCommand($container),
109
                $self->createReleaseAllCommand($container),
110
                $self->createValidateCommand($container),
111
                $self->createInitializeCommand($container),
112
            ];
113
        });
114
115
        return $container;
116
    }
117
118
    /**
119
     * @param \Pimple\Container $container
120
     *
121
     * @return \Dandelion\Console\Command\SplitCommand
122
     */
123
    protected function createSplitCommand(Container $container): SplitCommand
124
    {
125
        return new SplitCommand($container->offsetGet('splitter'));
126
    }
127
128
    /**
129
     * @param \Pimple\Container $container
130
     *
131
     * @return \Dandelion\Console\Command\SplitAllCommand
132
     */
133
    protected function createSplitAllCommand(Container $container): SplitAllCommand
134
    {
135
        return new SplitAllCommand($container->offsetGet('splitter'));
136
    }
137
138
    /**
139
     * @param \Pimple\Container $container
140
     *
141
     * @return \Pimple\Container
142
     */
143
    protected function registerSplitter(Container $container): Container
144
    {
145
        $container->offsetSet('splitter', static function (Container $container) {
146
            return new Splitter(
147
                $container->offsetGet('configuration_loader'),
148
                $container->offsetGet('process_pool_factory'),
149
                $container->offsetGet('result_factory'),
150
                $container->offsetGet('message_factory'),
151
                $container->offsetGet('git'),
152
                $container->offsetGet('splitsh_lite'),
153
                $container->offsetGet('bin_dir')
154
            );
155
        });
156
157
        return $container;
158
    }
159
160
    /**
161
     * @param \Pimple\Container $container
162
     *
163
     * @return \Pimple\Container
164
     */
165
    protected function registerGit(Container $container): Container
166
    {
167
        $container->offsetSet('git', static function (Container $container) {
168
            return new Git($container->offsetGet('process_factory'));
169
        });
170
171
        return $container;
172
    }
173
174
    /**
175
     * @param \Pimple\Container $container
176
     *
177
     * @return \Pimple\Container
178
     */
179
    protected function registerProcessFactory(Container $container): Container
180
    {
181
        $container->offsetSet('process_factory', static function () {
182
            return new ProcessFactory();
183
        });
184
185
        return $container;
186
    }
187
188
    /**
189
     * @param \Pimple\Container $container
190
     *
191
     * @return \Pimple\Container
192
     */
193
    protected function registerProcessPoolFactory(Container $container): Container
194
    {
195
        $container->offsetSet('process_pool_factory', static function (Container $container) {
196
            return new ProcessPoolFactory(
197
                $container->offsetGet('process_factory')
198
            );
199
        });
200
201
        return $container;
202
    }
203
204
    /**
205
     * @param \Pimple\Container $container
206
     *
207
     * @return \Pimple\Container
208
     */
209
    protected function registerResultFactory(Container $container): Container
210
    {
211
        $container->offsetSet('result_factory', static function () {
212
            return new ResultFactory();
213
        });
214
215
        return $container;
216
    }
217
218
    /**
219
     * @param \Pimple\Container $container
220
     *
221
     * @return \Pimple\Container
222
     */
223
    protected function registerMessageFactory(Container $container): Container
224
    {
225
        $container->offsetSet('message_factory', static function () {
226
            return new MessageFactory();
227
        });
228
229
        return $container;
230
    }
231
232
    /**
233
     * @param \Pimple\Container $container
234
     *
235
     * @return \Pimple\Container
236
     */
237
    protected function registerSplitshLite(Container $container): Container
238
    {
239
        $container->offsetSet('splitsh_lite', static function (Container $container) {
240
            return new SplitshLite(
241
                $container->offsetGet('process_factory'),
242
                $container->offsetGet('configuration_loader')
243
            );
244
        });
245
246
        return $container;
247
    }
248
249
    /**
250
     * @param \Pimple\Container $container
251
     *
252
     * @return \Pimple\Container
253
     */
254
    protected function registerConfigurationValidator(Container $container): Container
255
    {
256
        $container->offsetSet('configuration_validator', static function (Container $container) {
257
            $pathToDandelionSchema = sprintf('%sdandelion.schema.json', $container->offsetGet('resources_dir'));
258
259
            return new ConfigurationValidator(
260
                $container->offsetGet('configuration_loader'),
261
                Schema::import($pathToDandelionSchema)
262
            );
263
        });
264
265
        return $container;
266
    }
267
268
    /**
269
     * @param \Pimple\Container $container
270
     *
271
     * @return \Pimple\Container
272
     */
273
    protected function registerConfigurationLoader(Container $container): Container
274
    {
275
        $container->offsetSet('configuration_loader', static function (Container $container) {
276
            return new ConfigurationLoader(
277
                $container->offsetGet('configuration_finder'),
278
                $container->offsetGet('filesystem'),
279
                $container->offsetGet('serializer')
280
            );
281
        });
282
283
        return $container;
284
    }
285
286
    /**
287
     * @param \Pimple\Container $container
288
     *
289
     * @return \Pimple\Container
290
     */
291
    protected function registerConfigurationFinder(Container $container): Container
292
    {
293
        $container->offsetSet('configuration_finder', static function (Container $container) {
294
            return new ConfigurationFinder(
295
                $container->offsetGet('finder'),
296
                $container->offsetGet('filesystem')
297
            );
298
        });
299
300
        return $container;
301
    }
302
303
    /**
304
     * @param \Pimple\Container $container
305
     *
306
     * @return \Pimple\Container
307
     */
308
    protected function registerFinder(Container $container): Container
309
    {
310
        $container->offsetSet('finder', static function () {
311
            return new Finder();
312
        });
313
314
        return $container;
315
    }
316
317
    /**
318
     * @param \Pimple\Container $container
319
     *
320
     * @return \Pimple\Container
321
     */
322
    protected function registerFilesystem(Container $container): Container
323
    {
324
        $container->offsetSet('filesystem', static function () {
325
            return new Filesystem();
326
        });
327
328
        return $container;
329
    }
330
331
    /**
332
     * @param \Pimple\Container $container
333
     *
334
     * @return \Pimple\Container
335
     */
336
    protected function registerSerializer(Container $container): Container
337
    {
338
        $container->offsetSet('serializer', static function () {
339
            $normalizer = [
340
                new ObjectNormalizer(null, null, null, new PhpDocExtractor()),
341
                new ArrayDenormalizer()
342
            ];
343
344
            return new Serializer(
345
                $normalizer,
346
                [new JsonEncoder()]
347
            );
348
        });
349
350
        return $container;
351
    }
352
353
    /**
354
     * @param \Pimple\Container $container
355
     *
356
     * @return \Pimple\Container
357
     */
358
    protected function registerReleaser(Container $container): Container
359
    {
360
        $container->offsetSet('releaser', static function (Container $container) {
361
            return new Releaser(
362
                $container->offsetGet('configuration_loader'),
363
                $container->offsetGet('filesystem'),
364
                $container->offsetGet('process_pool_factory'),
365
                $container->offsetGet('result_factory'),
366
                $container->offsetGet('message_factory'),
367
                $container->offsetGet('git'),
368
                $container->offsetGet('bin_dir')
369
            );
370
        });
371
372
        return $container;
373
    }
374
375
    /**
376
     * @param \Pimple\Container $container
377
     *
378
     * @return \Pimple\Container
379
     */
380
    protected function registerInitializer(Container $container): Container
381
    {
382
        $container->offsetSet('initializer', static function (Container $container) {
383
            return new Initializer(
384
                $container->offsetGet('git'),
385
                $container->offsetGet('configuration_loader'),
386
                $container->offsetGet('message_factory'),
387
                $container->offsetGet('result_factory'),
388
            );
389
        });
390
391
        return $container;
392
    }
393
394
    /**
395
     * @param \Pimple\Container $container
396
     *
397
     * @return \Dandelion\Console\Command\ReleaseCommand
398
     */
399
    protected function createReleaseCommand(Container $container): ReleaseCommand
400
    {
401
        return new ReleaseCommand($container->offsetGet('releaser'));
402
    }
403
404
    /**
405
     * @param \Pimple\Container $container
406
     *
407
     * @return \Dandelion\Console\Command\ReleaseAllCommand
408
     */
409
    protected function createReleaseAllCommand(Container $container): ReleaseAllCommand
410
    {
411
        return new ReleaseAllCommand($container->offsetGet('releaser'));
412
    }
413
414
    /**
415
     * @param \Pimple\Container $container
416
     *
417
     * @return \Dandelion\Console\Command\ValidateCommand
418
     */
419
    protected function createValidateCommand(Container $container): ValidateCommand
420
    {
421
        return new ValidateCommand(
422
            $container->offsetGet('configuration_validator')
423
        );
424
    }
425
426
    protected function createInitializeCommand(Container $container): InitializeCommand
427
    {
428
        return new InitializeCommand($container->offsetGet('initializer'));
429
    }
430
}
431