Passed
Push — master ( 676110...9cfebc )
by Daniel
02:12
created

DandelionServiceProvider::createValidateCommand()   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\ReleaseAllCommand;
11
use Dandelion\Console\Command\ReleaseCommand;
12
use Dandelion\Console\Command\SplitAllCommand;
13
use Dandelion\Console\Command\SplitCommand;
14
use Dandelion\Console\Command\ValidateCommand;
15
use Dandelion\Environment\OperatingSystem;
16
use Dandelion\Filesystem\Filesystem;
17
use Dandelion\Operation\Releaser;
18
use Dandelion\Operation\Splitter;
19
use Dandelion\Process\ProcessFactory;
20
use Dandelion\VersionControl\Git;
21
use Dandelion\VersionControl\SplitshLite;
22
use Pimple\Container;
23
use Pimple\ServiceProviderInterface;
24
use Swaggest\JsonSchema\Schema;
25
use Symfony\Component\Finder\Finder;
26
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
27
use Symfony\Component\Serializer\Encoder\JsonEncoder;
28
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
29
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
30
use Symfony\Component\Serializer\Serializer;
31
32
use function rtrim;
33
use function sprintf;
34
use function str_repeat;
35
36
class DandelionServiceProvider implements ServiceProviderInterface
37
{
38
    /**
39
     * @param \Pimple\Container $container
40
     */
41
    public function register(Container $container): void
42
    {
43
        $container = $this->registerDirectoryPaths($container);
44
        $container = $this->registerFilesystem($container);
45
        $container = $this->registerFinder($container);
46
        $container = $this->registerSerializer($container);
47
        $container = $this->registerConfigurationLoader($container);
48
        $container = $this->registerConfigurationFinder($container);
49
        $container = $this->registerProcessFactory($container);
50
        $container = $this->registerOperatingSystem($container);
51
        $container = $this->registerGit($container);
52
        $container = $this->registerSplitshLite($container);
53
        $container = $this->registerSplitter($container);
54
        $container = $this->registerReleaser($container);
55
        $container = $this->registerConfigurationValidator($container);
56
57
        $this->registerCommands($container);
58
    }
59
60
    /**
61
     * @param \Pimple\Container $container
62
     *
63
     * @return \Pimple\Container
64
     */
65
    protected function registerDirectoryPaths(Container $container): Container
66
    {
67
        $rootDir = rtrim(__DIR__, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR
68
            . str_repeat('..' . DIRECTORY_SEPARATOR, 2);
69
70
        $container->offsetSet('root_dir', static function () use ($rootDir) {
71
            return $rootDir;
72
        });
73
74
        $container->offsetSet('src_dir', static function () use ($rootDir) {
75
            return sprintf('%ssrc%s', $rootDir, DIRECTORY_SEPARATOR);
76
        });
77
78
        $container->offsetSet('resources_dir', static function () use ($rootDir) {
79
            return sprintf('%sresources%s', $rootDir, DIRECTORY_SEPARATOR);
80
        });
81
82
        $container->offsetSet('bin_dir', static function () use ($rootDir) {
83
            return sprintf('%sbin%s', $rootDir, DIRECTORY_SEPARATOR);
84
        });
85
86
        return $container;
87
    }
88
89
    /**
90
     * @param \Pimple\Container $container
91
     *
92
     * @return \Pimple\Container
93
     */
94
    protected function registerCommands(Container $container): Container
95
    {
96
        $self = $this;
97
98
        $container->offsetSet('commands', static function (Container $container) use ($self) {
99
            return [
100
                $self->createSplitCommand($container),
101
                $self->createSplitAllCommand($container),
102
                $self->createReleaseCommand($container),
103
                $self->createReleaseAllCommand($container),
104
                $self->createValidateCommand($container),
105
            ];
106
        });
107
108
        return $container;
109
    }
110
111
    /**
112
     * @param \Pimple\Container $container
113
     *
114
     * @return \Dandelion\Console\Command\SplitCommand
115
     */
116
    protected function createSplitCommand(Container $container): SplitCommand
117
    {
118
        return new SplitCommand($container->offsetGet('splitter'));
119
    }
120
121
    /**
122
     * @param \Pimple\Container $container
123
     *
124
     * @return \Dandelion\Console\Command\SplitAllCommand
125
     */
126
    protected function createSplitAllCommand(Container $container): SplitAllCommand
127
    {
128
        return new SplitAllCommand($container->offsetGet('splitter'));
129
    }
130
131
    /**
132
     * @param \Pimple\Container $container
133
     *
134
     * @return \Pimple\Container
135
     */
136
    protected function registerSplitter(Container $container): Container
137
    {
138
        $container->offsetSet('splitter', static function (Container $container) {
139
            return new Splitter(
140
                $container->offsetGet('configuration_loader'),
141
                $container->offsetGet('process_factory'),
142
                $container->offsetGet('git'),
143
                $container->offsetGet('splitsh_lite'),
144
                $container->offsetGet('bin_dir')
145
            );
146
        });
147
148
        return $container;
149
    }
150
151
    /**
152
     * @param \Pimple\Container $container
153
     *
154
     * @return \Pimple\Container
155
     */
156
    protected function registerGit(Container $container): Container
157
    {
158
        $container->offsetSet('git', static function (Container $container) {
159
            return new Git($container->offsetGet('process_factory'));
160
        });
161
162
        return $container;
163
    }
164
165
    /**
166
     * @param \Pimple\Container $container
167
     *
168
     * @return \Pimple\Container
169
     */
170
    protected function registerProcessFactory(Container $container): Container
171
    {
172
        $container->offsetSet('process_factory', static function () {
173
            return new ProcessFactory();
174
        });
175
176
        return $container;
177
    }
178
179
    /**
180
     * @param \Pimple\Container $container
181
     *
182
     * @return \Pimple\Container
183
     */
184
    protected function registerSplitshLite(Container $container): Container
185
    {
186
        $container->offsetSet('splitsh_lite', static function (Container $container) {
187
            return new SplitshLite(
188
                $container->offsetGet('operating_system'),
189
                $container->offsetGet('process_factory'),
190
                $container->offsetGet('bin_dir')
191
            );
192
        });
193
194
        return $container;
195
    }
196
197
    /**
198
     * @param \Pimple\Container $container
199
     *
200
     * @return \Pimple\Container
201
     */
202
    protected function registerOperatingSystem(Container $container): Container
203
    {
204
        $container->offsetSet('operating_system', static function () {
205
            return new OperatingSystem();
206
        });
207
208
        return $container;
209
    }
210
211
    /**
212
     * @param \Pimple\Container $container
213
     *
214
     * @return \Pimple\Container
215
     */
216
    protected function registerConfigurationValidator(Container $container): Container
217
    {
218
        $container->offsetSet('configuration_validator', static function (Container $container) {
219
            $pathToDandelionSchema = sprintf('%sdandelion.schema.json', $container->offsetGet('resources_dir'));
220
221
            return new ConfigurationValidator(
222
                $container->offsetGet('configuration_loader'),
223
                Schema::import($pathToDandelionSchema)
224
            );
225
        });
226
227
        return $container;
228
    }
229
230
    /**
231
     * @param \Pimple\Container $container
232
     *
233
     * @return \Pimple\Container
234
     */
235
    protected function registerConfigurationLoader(Container $container): Container
236
    {
237
        $container->offsetSet('configuration_loader', static function (Container $container) {
238
            return new ConfigurationLoader(
239
                $container->offsetGet('configuration_finder'),
240
                $container->offsetGet('filesystem'),
241
                $container->offsetGet('serializer')
242
            );
243
        });
244
245
        return $container;
246
    }
247
248
    /**
249
     * @param \Pimple\Container $container
250
     *
251
     * @return \Pimple\Container
252
     */
253
    protected function registerConfigurationFinder(Container $container): Container
254
    {
255
        $container->offsetSet('configuration_finder', static function (Container $container) {
256
            return new ConfigurationFinder(
257
                $container->offsetGet('finder'),
258
                $container->offsetGet('filesystem')
259
            );
260
        });
261
262
        return $container;
263
    }
264
265
    /**
266
     * @param \Pimple\Container $container
267
     *
268
     * @return \Pimple\Container
269
     */
270
    protected function registerFinder(Container $container): Container
271
    {
272
        $container->offsetSet('finder', static function () {
273
            return new Finder();
274
        });
275
276
        return $container;
277
    }
278
279
    /**
280
     * @param \Pimple\Container $container
281
     *
282
     * @return \Pimple\Container
283
     */
284
    protected function registerFilesystem(Container $container): Container
285
    {
286
        $container->offsetSet('filesystem', static function () {
287
            return new Filesystem();
288
        });
289
290
        return $container;
291
    }
292
293
    /**
294
     * @param \Pimple\Container $container
295
     *
296
     * @return \Pimple\Container
297
     */
298
    protected function registerSerializer(Container $container): Container
299
    {
300
        $container->offsetSet('serializer', static function () {
301
            $normalizer = [
302
                new ObjectNormalizer(null, null, null, new PhpDocExtractor()),
303
                new ArrayDenormalizer()
304
            ];
305
306
            return new Serializer(
307
                $normalizer,
308
                [new JsonEncoder()]
309
            );
310
        });
311
312
        return $container;
313
    }
314
315
    /**
316
     * @param \Pimple\Container $container
317
     *
318
     * @return \Pimple\Container
319
     */
320
    protected function registerReleaser(Container $container): Container
321
    {
322
        $container->offsetSet('releaser', static function (Container $container) {
323
            return new Releaser(
324
                $container->offsetGet('configuration_loader'),
325
                $container->offsetGet('filesystem'),
326
                $container->offsetGet('process_factory'),
327
                $container->offsetGet('git'),
328
                $container->offsetGet('bin_dir')
329
            );
330
        });
331
332
        return $container;
333
    }
334
335
    /**
336
     * @param \Pimple\Container $container
337
     *
338
     * @return \Dandelion\Console\Command\ReleaseCommand
339
     */
340
    protected function createReleaseCommand(Container $container): ReleaseCommand
341
    {
342
        return new ReleaseCommand($container->offsetGet('releaser'));
343
    }
344
345
    /**
346
     * @param \Pimple\Container $container
347
     *
348
     * @return \Dandelion\Console\Command\ReleaseAllCommand
349
     */
350
    protected function createReleaseAllCommand(Container $container): ReleaseAllCommand
351
    {
352
        return new ReleaseAllCommand($container->offsetGet('releaser'));
353
    }
354
355
    protected function createValidateCommand(Container $container): ValidateCommand
356
    {
357
        return new ValidateCommand($container->offsetGet('configuration_validator'));
358
    }
359
}
360