ExceptionDescriberCompilerPass::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 19
cts 19
cp 1
rs 9.488
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Faecie\Bundle\JsonApiErrorResponseBundle\DependencyInjection\Compiler;
6
7
use Faecie\Bundle\JsonApiErrorResponseBundle\ExceptionDescriber\DescriptiveExceptionDescriber;
8
use Faecie\Bundle\JsonApiErrorResponseBundle\ExceptionDescriber\ExceptionDescriberInterface;
9
use Faecie\Bundle\JsonApiErrorResponseBundle\ExceptionDescriber\ExceptionDescribersQueue;
10
use Faecie\Bundle\JsonApiErrorResponseBundle\ExceptionDescriber\SymfonyExceptionDescriber;
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
/**
17
 * Class ExceptionDescriberCompilerPass
18
 */
19
class ExceptionDescriberCompilerPass implements CompilerPassInterface
20
{
21
    /**
22
     * Tag for services
23
     */
24
    public const TAG_NAME = 'json_api.exception_describer';
25
26 3
    public function process(ContainerBuilder $container)
27
    {
28 3
        $references = [];
29 3
        $references[] = new Reference(DescriptiveExceptionDescriber::class);
30 3
        foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $serviceId => $tagsAttributes) {
31 2
            $definition = $container->findDefinition($serviceId);
32 2
            $class = $definition->getClass();
33 2
            $implements = class_implements($definition->getClass());
34 2
            if (! isset($implements[ExceptionDescriberInterface::class])) {
35 1
                throw new InvalidArgumentException(
36 1
                    sprintf(
37 1
                        'Service "%s" with class "%s" must implement interface "%s"',
38 1
                        $serviceId,
39 1
                        $class,
40 1
                        ExceptionDescriberInterface::class
41
                    )
42
                );
43
            }
44
45 1
            $references[] = new Reference($serviceId);
46
        }
47
48 2
        $references[] = new Reference(SymfonyExceptionDescriber::class);
49
50 2
        $container->getDefinition(ExceptionDescribersQueue::class)
51 2
            ->replaceArgument(0, $references);
52 2
    }
53
}
54