Passed
Pull Request — master (#15)
by Daniel
02:49
created

createSplitRepositoryInitAllCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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