GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Container::configureValidationComponents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1.037

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
ccs 6
cts 9
cp 0.6667
crap 1.037
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EdmondsCommerce\DoctrineStaticMeta;
6
7
// phpcs:disable
8
use Doctrine\Common\Cache\ArrayCache;
9
use Doctrine\Common\Cache\Cache;
10
use Doctrine\Common\Cache\FilesystemCache;
11
use Doctrine\DBAL\Migrations\Tools\Console\Command\DiffCommand;
12
use Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand;
13
use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand;
14
use Doctrine\DBAL\Migrations\Tools\Console\Command\LatestCommand;
15
use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand;
16
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand;
17
use Doctrine\DBAL\Migrations\Tools\Console\Command\UpToDateCommand;
18
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Doctrine\ORM\Tools\SchemaTool;
21
use Doctrine\ORM\Tools\SchemaValidator;
22
use EdmondsCommerce\DoctrineStaticMeta\Builder\Builder;
23
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateConstraintAction;
24
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateDbalFieldAndInterfaceAction;
25
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateDtosForAllEntitiesAction;
26
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateEmbeddableAction;
27
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action\CreateEntityAction;
28
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
29
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\CliConfigCommandFactory;
30
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\CreateConstraintCommand;
31
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\FinaliseBuildCommand;
32
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateEmbeddableFromArchetypeCommand;
33
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateEmbeddableSkeletonCommand;
34
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateEntityCommand;
35
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateFieldCommand;
36
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\GenerateRelationsCommand;
37
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\OverrideCreateCommand;
38
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\OverridesUpdateCommand;
39
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\RemoveUnusedRelationsCommand;
40
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\SetEmbeddableCommand;
41
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\SetFieldCommand;
42
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\SetRelationCommand;
43
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entities\EntityCreator;
44
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\DataTransferObjects\DtoCreator;
45
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\FakerData\EmbeddableFakerDataCreator;
46
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Interfaces\HasEmbeddableInterfaceCreator;
47
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Interfaces\Objects\EmbeddableInterfaceCreator;
48
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Objects\EmbeddableCreator;
49
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Embeddable\Traits\HasEmbeddableTraitCreator;
50
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Factories\AbstractEntityFactoryCreator;
51
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Factories\EntityDtoFactoryCreator;
52
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Factories\EntityFactoryCreator;
53
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Interfaces\FieldInterfaceCreator;
54
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Fields\Traits\FieldTraitCreator;
55
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Interfaces\EntityInterfaceCreator;
56
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Repositories\AbstractEntityRepositoryCreator;
57
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Repositories\EntityRepositoryCreator;
58
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Savers\EntitySaverCreator;
59
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Savers\EntityUnitOfWorkHelperCreator;
60
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\Savers\EntityUpserterCreator;
61
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintCreator;
62
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\EntityIsValidConstraintValidatorCreator;
63
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintCreator;
64
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Validation\Constraints\PropertyConstraintValidatorCreator;
65
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\Assets\Entity\Fixtures\EntityFixtureCreator;
66
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\BootstrapCreator;
67
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\Entities\AbstractEntityTestCreator;
68
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Tests\Entities\EntityTestCreator;
69
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FileFactory;
70
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\Factory\FindReplaceFactory;
71
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem\File\Writer;
72
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable\ArchetypeEmbeddableGenerator;
73
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable\EntityEmbeddableSetter;
74
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\EntityGenerator;
75
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\AbstractTestFakerDataProviderUpdater;
76
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\EntityFieldSetter;
77
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\FieldGenerator;
78
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait;
79
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\StandardLibraryTestGenerator;
80
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FileCreationTransaction;
81
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FindAndReplaceHelper;
82
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator;
83
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
84
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PathHelper;
85
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\CopyPhpstormMeta;
86
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\EntityFormatter;
87
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider;
88
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\ReflectionHelper;
89
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper;
90
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\UnusedRelationsRemover;
91
use EdmondsCommerce\DoctrineStaticMeta\Entity\DataTransferObjects\DtoFactory;
92
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityDependencyInjector;
93
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactory;
94
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactoryInterface;
95
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories\UuidFactory;
96
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityDataValidatorInterface;
97
use EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories\RepositoryFactory;
98
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntitySaver;
99
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkEntityUpdater;
100
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\BulkSimpleEntityCreator;
101
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver;
102
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
103
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\FakerDataFillerFactory;
104
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory;
105
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelperFactory;
106
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityDataValidator;
107
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityDataValidatorFactory;
108
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\Initialiser;
109
use EdmondsCommerce\DoctrineStaticMeta\EntityManager\EntityManagerFactory;
110
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
111
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database;
112
use EdmondsCommerce\DoctrineStaticMeta\Schema\MysqliConnectionFactory;
113
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema;
114
use EdmondsCommerce\DoctrineStaticMeta\Schema\UuidFunctionPolyfill;
115
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\TestCodeGenerator;
116
use ProjectServiceContainer;
117
use Psr\Container\ContainerExceptionInterface;
118
use Psr\Container\ContainerInterface;
119
use Psr\Container\NotFoundExceptionInterface;
120
use Symfony\Component\DependencyInjection\ContainerBuilder;
121
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...ection\Dumper\PhpDumper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
122
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
123
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
124
use Symfony\Component\DependencyInjection\Reference;
125
use Symfony\Component\Filesystem\Filesystem;
126
use Symfony\Component\Finder\Finder;
127
use Symfony\Component\Finder\SplFileInfo;
128
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
129
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
130
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
131
132
// phpcs:enable
133
134
/**
135
 * Class Container
136
 *
137
 * @package EdmondsCommerce\DoctrineStaticMeta
138
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
139
 */
140
class Container implements ContainerInterface
141
{
142
    /**
143
     * This is the list of services managed by this container
144
     *
145
     * This list is used to also generate a PHPStorm meta data file which assists with dynamic type hinting when using
146
     * the container as a service locator
147
     *
148
     * @see ./../../.phpstorm.meta.php/container.meta.php
149
     */
150
    public const SERVICES = [
151
        \Ramsey\Uuid\UuidFactory::class,
152
        AbstractEntityFactoryCreator::class,
153
        AbstractEntityRepositoryCreator::class,
154
        AbstractEntityTestCreator::class,
155
        AbstractTestFakerDataProviderUpdater::class,
156
        ArchetypeEmbeddableGenerator::class,
157
        ArrayCache::class,
158
        BootstrapCreator::class,
159
        Builder::class,
160
        BulkEntitySaver::class,
161
        BulkEntityUpdater::class,
162
        BulkSimpleEntityCreator::class,
163
        CliConfigCommandFactory::class,
164
        CodeHelper::class,
165
        Config::class,
166
        ContainerConstraintValidatorFactory::class,
167
        CopyPhpstormMeta::class,
168
        CreateConstraintAction::class,
169
        CreateConstraintCommand::class,
170
        CreateDtosForAllEntitiesAction::class,
171
        CreateEmbeddableAction::class,
172
        CreateEntityAction::class,
173
        Database::class,
174
        DiffCommand::class,
175
        DoctrineCache::class,
176
        DtoCreator::class,
177
        DtoFactory::class,
178
        EmbeddableCreator::class,
179
        EmbeddableFakerDataCreator::class,
180
        EmbeddableInterfaceCreator::class,
181
        EntityCreator::class,
182
        EntityDataValidator::class,
183
        EntityDataValidatorFactory::class,
184
        EntityDependencyInjector::class,
185
        EntityDtoFactoryCreator::class,
186
        EntityEmbeddableSetter::class,
187
        EntityFactory::class,
188
        EntityFactoryCreator::class,
189
        EntityFieldSetter::class,
190
        EntityFixtureCreator::class,
191
        EntityFormatter::class,
192
        EntityGenerator::class,
193
        EntityInterfaceCreator::class,
194
        EntityIsValidConstraintCreator::class,
195
        EntityIsValidConstraintValidatorCreator::class,
196
        EntityManagerFactory::class,
197
        EntityManagerInterface::class,
198
        EntityRepositoryCreator::class,
199
        EntitySaver::class,
200
        EntitySaverCreator::class,
201
        EntitySaverFactory::class,
202
        EntityTestCreator::class,
203
        EntityUnitOfWorkHelperCreator::class,
204
        EntityUpserterCreator::class,
205
        ExecuteCommand::class,
206
        FakerDataFillerFactory::class,
207
        FieldGenerator::class,
208
        FileCreationTransaction::class,
209
        FileFactory::class,
210
        FileOverrider::class,
211
        Filesystem::class,
212
        FilesystemCache::class,
213
        FinaliseBuildCommand::class,
214
        FindAndReplaceHelper::class,
215
        FindReplaceFactory::class,
216
        CreateDbalFieldAndInterfaceAction::class,
217
        FieldTraitCreator::class,
218
        FieldInterfaceCreator::class,
219
        Finder::class,
220
        FixturesHelperFactory::class,
221
        GenerateCommand::class,
222
        GenerateEmbeddableFromArchetypeCommand::class,
223
        GenerateEmbeddableSkeletonCommand::class,
224
        GenerateEntityCommand::class,
225
        GenerateFieldCommand::class,
226
        GenerateRelationsCommand::class,
227
        HasEmbeddableInterfaceCreator::class,
228
        HasEmbeddableTraitCreator::class,
229
        IdTrait::class,
230
        LatestCommand::class,
231
        MigrateCommand::class,
232
        MysqliConnectionFactory::class,
233
        NamespaceHelper::class,
234
        OverrideCreateCommand::class,
235
        OverridesUpdateCommand::class,
236
        PathHelper::class,
237
        PropertyConstraintCreator::class,
238
        PropertyConstraintValidatorCreator::class,
239
        ReflectionHelper::class,
240
        RelationsGenerator::class,
241
        RelationshipHelper::class,
242
        RemoveUnusedRelationsCommand::class,
243
        RepositoryFactory::class,
244
        Schema::class,
245
        SchemaTool::class,
246
        SchemaValidator::class,
247
        SetEmbeddableCommand::class,
248
        SetFieldCommand::class,
249
        SetRelationCommand::class,
250
        StandardLibraryTestGenerator::class,
251
        StatusCommand::class,
252
        TestCodeGenerator::class,
253
        TestEntityGeneratorFactory::class,
254
        TypeHelper::class,
255
        UnusedRelationsRemover::class,
256
        UpToDateCommand::class,
257
        UuidFactory::class,
258
        UuidFunctionPolyfill::class,
259
        VersionCommand::class,
260
        Writer::class,
261
        Initialiser::class,
262
    ];
263
264
    public const ALIASES = [
265
        EntityFactoryInterface::class              => EntityFactory::class,
266
        EntityDataValidatorInterface::class        => EntityDataValidator::class,
267
        ConstraintValidatorFactoryInterface::class => ContainerConstraintValidatorFactory::class,
268
    ];
269
270
    public const NOT_SHARED_SERVICES = [
271
    ];
272
273
274
    /**
275
     * The directory that container cache files will be stored
276
     */
277
    public const CACHE_PATH = __DIR__ . '/../cache/';
278
279
    public const SYMFONY_CACHE_PATH = self::CACHE_PATH . '/container.symfony.php';
280
281
    /**
282
     * @var bool
283
     */
284
    private $useCache = false;
285
286
    /**
287
     * @var ContainerInterface
288
     */
289
    private $container;
290
291
    /**
292
     * @param bool $useCache
293
     *
294
     * @return Container
295
     */
296
    public function setUseCache(bool $useCache): Container
297
    {
298
        $this->useCache = $useCache;
299
300
        return $this;
301
    }
302
303
    /**
304
     * @param array $server - normally you would pass in $_SERVER
305
     *
306
     * @throws DoctrineStaticMetaException
307
     */
308 2
    public function buildSymfonyContainer(array $server): void
309
    {
310 2
        if (true === $this->useCache && file_exists(self::SYMFONY_CACHE_PATH)) {
311
            /** @noinspection PhpIncludeInspection */
312
            require self::SYMFONY_CACHE_PATH;
313
            $this->setContainer(new ProjectServiceContainer());
314
315
            return;
316
        }
317
318
        try {
319 2
            $container = new ContainerBuilder();
320 2
            $this->addConfiguration($container, $server);
321 2
            $container->compile();
322 2
            $this->setContainer($container);
323 2
            $dumper = new PhpDumper($container);
324 2
            file_put_contents(self::SYMFONY_CACHE_PATH, $dumper->dump());
325
        } catch (ServiceNotFoundException | InvalidArgumentException $e) {
326
            throw new DoctrineStaticMetaException(
327
                'Exception building the container: ' . $e->getMessage(),
328
                $e->getCode(),
329
                $e
330
            );
331
        }
332 2
    }
333
334
    /**
335
     * Set a container instance
336
     *
337
     * @param ContainerInterface $container
338
     *
339
     * @return $this
340
     */
341 2
    public function setContainer(ContainerInterface $container): self
342
    {
343 2
        $this->container = $container;
344
345 2
        return $this;
346
    }
347
348
    /**
349
     * Build all the definitions, alias and other configuration for this container. Each of these steps need to be
350
     * carried out to allow the everything to work, however you may wish to change individual bits. Therefore this
351
     * method has been made final, but the individual methods can be overwritten if you extend off the class
352
     *
353
     * @param ContainerBuilder $containerBuilder
354
     * @param array            $server
355
     *
356
     * @throws InvalidArgumentException
357
     * @throws ServiceNotFoundException
358
     */
359 2
    final public function addConfiguration(ContainerBuilder $containerBuilder, array $server): void
360
    {
361 2
        $this->autoWireServices($containerBuilder);
362 2
        $this->defineConfig($containerBuilder, $server);
363 2
        $this->defineCache($containerBuilder, $server);
364 2
        $this->defineEntityManager($containerBuilder);
365 2
        $this->configureValidationComponents($containerBuilder);
366 2
        $this->defineAliases($containerBuilder);
367 2
        $this->registerCustomFakerDataFillers($containerBuilder);
368 2
    }
369
370
    /**
371
     * This takes every class from the getServices method, auto wires them and marks them as public. You may wish to
372
     * override this if you want to mark certain classes as private
373
     *
374
     * @param ContainerBuilder $containerBuilder
375
     */
376 2
    public function autoWireServices(ContainerBuilder $containerBuilder): void
377
    {
378 2
        $services = $this->getServices();
379 2
        foreach ($services as $class) {
380 2
            $containerBuilder->autowire($class, $class)->setPublic(true);
381
        }
382 2
    }
383
384
    /**
385
     * This is a simple wrapper around the class constants. You can use this to add, remove, or replace individual
386
     * services that will be auto wired
387
     *
388
     * @return array
389
     */
390 2
    public function getServices(): array
391
    {
392 2
        return self::SERVICES;
393
    }
394
395
    /**
396
     * This is used to auto wire the config interface. It sets the $server param as a constructor argument and then
397
     * sets the concrete class as the implementation for the Interface. Override this if you wish to use different
398
     * logic for where the config comes from
399
     *
400
     * @param ContainerBuilder $containerBuilder
401
     * @param array            $server
402
     */
403 2
    public function defineConfig(ContainerBuilder $containerBuilder, array $server): void
404
    {
405 2
        $containerBuilder->getDefinition(Config::class)->setArgument('$server', $this->configVars($server));
406 2
        $containerBuilder->setAlias(ConfigInterface::class, Config::class);
407 2
    }
408
409
    /**
410
     * Take the $server array, normally a copy of $_SERVER, and pull out just the bits required by config
411
     *
412
     * @param array $server
413
     *
414
     * @return array
415
     */
416 2
    protected function configVars(array $server): array
417
    {
418 2
        $return = array_intersect_key(
419 2
            $server,
420 2
            array_flip(ConfigInterface::PARAMS)
421
        );
422
423 2
        return $return;
424
    }
425
426
    /**
427
     * This is used to auto wire the doctrine cache. If we are in dev mode then this will always use the Array Cache,
428
     * if not then the cache will be set to what is in the $server array. Override this method if you wish to use
429
     * different logic to handle caching
430
     *
431
     * @param ContainerBuilder $containerBuilder
432
     * @param array            $server
433
     */
434 2
    public function defineCache(ContainerBuilder $containerBuilder, array $server): void
435
    {
436 2
        $cacheDriver = $server[Config::PARAM_DOCTRINE_CACHE_DRIVER] ?? Config::DEFAULT_DOCTRINE_CACHE_DRIVER;
437 2
        $containerBuilder->autowire($cacheDriver)->setPublic(true);
438 2
        $this->configureFilesystemCache($containerBuilder);
439
        /**
440
         * Which Cache Driver is used for the Cache Interface?
441
         *
442
         * If Dev mode, we always use the Array Cache
443
         *
444
         * Otherwise, we use the Configured Cache driver (which defaults to Array Cache)
445
         */
446 2
        $cache = ($server[Config::PARAM_DEVMODE] ?? false) ? ArrayCache::class : $cacheDriver;
447 2
        $containerBuilder->setAlias(Cache::class, $cache)->setPublic(true);
448 2
        $containerBuilder->getDefinition(DoctrineCache::class)->addArgument(new Reference($cache))->setPublic(true);
449 2
    }
450
451 2
    private function configureFilesystemCache(ContainerBuilder $containerBuilder): void
452
    {
453 2
        $config = $this->getConfig($containerBuilder);
454 2
        $containerBuilder->getDefinition(FilesystemCache::class)
455 2
                         ->addArgument($config->get(Config::PARAM_FILESYSTEM_CACHE_PATH))
456 2
                         ->setPublic(true);
457 2
    }
458
459 2
    private function getConfig(ContainerBuilder $containerBuilder): Config
460
    {
461 2
        return $containerBuilder->get(Config::class);
462
    }
463
464
    /**
465
     * This is used to auto wire the entity manager. It first adds the DSM factory as the factory for the class, and
466
     * sets the Entity Manager as the implementation of the interface. Overrider this if you want to use your own
467
     * factory to create and configure the entity manager
468
     *
469
     * @param ContainerBuilder $container
470
     */
471 2
    public function defineEntityManager(ContainerBuilder $container): void
472
    {
473 2
        $container->getDefinition(EntityManagerInterface::class)
474 2
                  ->addArgument(new Reference(Config::class))
475 2
                  ->setFactory(
476
                      [
477 2
                          new Reference(EntityManagerFactory::class),
478 2
                          'getEntityManager',
479
                      ]
480
                  );
481 2
    }
482
483
    /**
484
     * Ensure we are using the container constraint validator factory so that custom validators with dependencies can
485
     * simply declare them as normal. Note that you will need to define each custom validator as a service in your
486
     * container.
487
     *
488
     * @param ContainerBuilder $containerBuilder
489
     */
490 2
    public function configureValidationComponents(ContainerBuilder $containerBuilder): void
491
    {
492 2
        $containerBuilder->getDefinition(EntityDataValidator::class)
493 2
                         ->setFactory(
494
                             [
495 2
                                 new Reference(EntityDataValidatorFactory::class),
496 2
                                 'buildEntityDataValidator',
497
                             ]
498 2
                         )->setShared(false);
499 2
    }
500
501 2
    public function defineAliases(ContainerBuilder $containerBuilder): void
502
    {
503 2
        foreach (self::ALIASES as $interface => $service) {
504 2
            $containerBuilder->setAlias($interface, $service)->setPublic(true);
505
        }
506 2
    }
507
508 2
    private function registerCustomFakerDataFillers(ContainerBuilder $containerBuilder): void
509
    {
510 2
        $config = $this->getConfig($containerBuilder);
511 2
        $path   = $config->get(Config::PARAM_ENTITIES_CUSTOM_DATA_FILLER_PATH);
512 2
        if (!is_dir($path)) {
513 2
            return;
514
        }
515
        /** @var Finder $finder */
516
        $finder        = $containerBuilder->get(Finder::class);
517
        $files         = $finder->files()->name('*FakerDataFiller.php')->in($path);
518
        $baseNameSpace = $config->get(Config::PARAM_PROJECT_ROOT_NAMESPACE);
519
        $mappings      = [];
520
        foreach ($files as $file) {
521
            /** @var SplFileInfo $file */
522
            $dataFillerClassName = $baseNameSpace . '\\Assets\\Entity\\FakerDataFillers';
523
            $entityClassName     = $baseNameSpace . '\\Entities';
524
            $relativePath        = str_replace('/', '\\', $file->getRelativePath());
525
            if ($relativePath !== '') {
526
                $dataFillerClassName .= '\\' . $relativePath;
527
                $entityClassName     .= '\\' . $relativePath;
528
            }
529
            $fileName                   = $file->getBasename('.php');
530
            $dataFillerClassName        .= '\\' . $fileName;
531
            $entityClassName            .= '\\' . str_replace('FakerDataFiller', '', $fileName);
532
            $mappings[$entityClassName] = $dataFillerClassName;
533
        }
534
535
        $containerBuilder->getDefinition(FakerDataFillerFactory::class)
536
                         ->addMethodCall('setCustomFakerDataFillersFqns', [$mappings]);
537
    }
538
539
    /**
540
     * Some service should not be Singletons (shared) but should always be a new instance
541
     *
542
     * @param ContainerBuilder $containerBuilder
543
     */
544
    public function updateNotSharedServices(ContainerBuilder $containerBuilder): void
545
    {
546
        foreach (self::NOT_SHARED_SERVICES as $service) {
547
            $containerBuilder->getDefinition($service)->setShared(false);
548
        }
549
    }
550
551
    /**
552
     * @param string $id
553
     *
554
     * @return mixed
555
     * @SuppressWarnings(PHPMD.ShortVariable)
556
     * @throws DoctrineStaticMetaException
557
     */
558 2
    public function get($id)
559
    {
560
        try {
561 2
            return $this->container->get($id);
562
        } catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
563
            throw new DoctrineStaticMetaException(
564
                get_class($e) . ' getting service ' . $id . ': ' . $e->getMessage(),
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

564
                get_class($e) . ' getting service ' . $id . ': ' . $e->/** @scrutinizer ignore-call */ getMessage(),
Loading history...
565
                $e->getCode(),
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

565
                $e->/** @scrutinizer ignore-call */ 
566
                    getCode(),
Loading history...
566
                $e
567
            );
568
        }
569
    }
570
571
    /**
572
     * @param string $id
573
     * @SuppressWarnings(PHPMD.ShortVariable)
574
     *
575
     * @return bool|void
576
     */
577
    public function has($id)
578
    {
579
        return $this->container->has($id);
580
    }
581
}
582