RegisterFactoriesPass   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 8 2
A registerFactory() 0 14 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\DAOBundle\DependencyInjection\Compiler;
6
7
use Nyholm\Psr7\Factory\Psr17Factory;
8
use Psr\Http\Message\RequestFactoryInterface;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
12
13
final class RegisterFactoriesPass implements CompilerPassInterface
14
{
15
    private const PSR17_FACTORY_SERVICE_ID = 'setono_dao.psr17_factory';
16
17
    public function process(ContainerBuilder $container): void
18
    {
19
        if (class_exists(Psr17Factory::class)) {
20
            // this service is used later if the Psr17Factory exists. Else it will be automatically removed by Symfony
21
            $container->register(self::PSR17_FACTORY_SERVICE_ID, Psr17Factory::class);
22
        }
23
24
        $this->registerFactory($container, 'setono_dao.request_factory', 'setono_dao.request_factory', RequestFactoryInterface::class);
25
    }
26
27
    private function registerFactory(ContainerBuilder $container, string $parameter, string $service, string $factoryInterface): void
28
    {
29
        if ($container->hasParameter($parameter)) {
30
            if (!$container->has($container->getParameter($parameter))) {
0 ignored issues
show
Bug introduced by
It seems like $container->getParameter($parameter) can also be of type array; however, parameter $id of Symfony\Component\Depend...ContainerBuilder::has() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

30
            if (!$container->has(/** @scrutinizer ignore-type */ $container->getParameter($parameter))) {
Loading history...
31
                throw new ServiceNotFoundException($container->getParameter($parameter));
32
            }
33
34
            $container->setAlias($service, $container->getParameter($parameter));
0 ignored issues
show
Bug introduced by
It seems like $container->getParameter($parameter) can also be of type array; however, parameter $id of Symfony\Component\Depend...inerBuilder::setAlias() does only seem to accept Symfony\Component\DependencyInjection\Alias|string, maybe add an additional type check? ( Ignorable by Annotation )

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

34
            $container->setAlias($service, /** @scrutinizer ignore-type */ $container->getParameter($parameter));
Loading history...
35
        } elseif ($container->has($factoryInterface)) {
36
            $container->setAlias($service, $factoryInterface);
37
        } elseif ($container->has('nyholm.psr7.psr17_factory')) {
38
            $container->setAlias($service, 'nyholm.psr7.psr17_factory');
39
        } elseif (class_exists(Psr17Factory::class)) {
40
            $container->setAlias($service, self::PSR17_FACTORY_SERVICE_ID);
41
        }
42
    }
43
}
44