Passed
Push — master ( ef92a5...f096ba )
by Gerrit
02:09
created

ControllerErrorHandlingPass::process()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 18
cts 18
cp 1
rs 9.0008
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\DependencyInjection\Compiler;
14
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Webmozart\Assert\Assert;
19
20
final class ControllerErrorHandlingPass implements CompilerPassInterface
21
{
22
23 2
    public function process(ContainerBuilder $container): void
24
    {
25
        /** @var array<string, array> $taggedErrorHandlers */
26 2
        $taggedErrorHandlers = $container->findTaggedServiceIds("symfony_generics.error_handler");
27
28 2
        foreach ($taggedErrorHandlers as $errorHandlerServiceId => $errorHandlerTags) {
29 2
            foreach ($errorHandlerTags as $errorHandlerTag) {
30 2
                Assert::keyExists($errorHandlerTag, 'key');
31
32
                /** @var string $errorHandlerKey */
33 1
                $errorHandlerKey = $errorHandlerTag['key'];
34
35 1
                unset($errorHandlerTag['key']);
36
37
                /** @var array<string, array> $taggedControllers */
38 1
                $taggedControllers = $container->findTaggedServiceIds(sprintf(
39 1
                    "symfony_generics.error_handler.%s",
40 1
                    $errorHandlerKey
41
                ));
42
43
                /** @var Definition $errorHandlerDefinition */
44 1
                $errorHandlerDefinition = $container->getDefinition($errorHandlerServiceId);
45
46 1
                foreach ($taggedControllers as $controllerServiceId => $tags) {
47
                    /** @var array $tags */
48
49 1
                    foreach ($tags as $tagData) {
50
                        /** @var array $tagData */
51 1
                        $tagData = array_merge([
52 1
                            'decorates' => $controllerServiceId
53 1
                        ], $errorHandlerTag, $tagData);
54
55 1
                        $errorHandlerDefinition->addTag("symfony_generics.decorates", $tagData);
56
                    }
57
                }
58
            }
59
        }
60 1
    }
61
62
}
63