SettingsAwarePass   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 29
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 26 6
1
<?php
2
3
namespace DoS\ResourceBundle\DependencyInjection\Compiler;
4
5
use DoS\ResourceBundle\Settings\SettingsAwareInterface;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class SettingsAwarePass implements CompilerPassInterface
11
{
12
    public function process(ContainerBuilder $container)
13
    {
14
        if (!$container->hasDefinition('sylius.settings.manager')) {
15
            return;
16
        }
17
18
        foreach ($container->findTaggedServiceIds('dos.settings_aware') as $id => $attributes) {
19
            if (!array_key_exists('alias', $attributes[0])) {
20
                throw new \InvalidArgumentException(sprintf('Service "%s" must define the "alias" attribute on "dos.settings_aware" tags.', $id));
21
            }
22
23
            $alias = $attributes[0]['alias'];
24
            $service = $container->getDefinition($id);
25
            $class = $service->getClass();
26
27
            if (strpos($class, '%') !== false) {
28
                $class = $container->getParameter(str_replace('%', '', $class));
29
            }
30
31
            if (!in_array(SettingsAwareInterface::class, class_implements($class))) {
32
                throw new \InvalidArgumentException(sprintf('Service "%s" must be implemented of the "%s".', $id, SettingsAwareInterface::class));
33
            }
34
35
            $service->addMethodCall('setSettings', array(new Reference('sylius.settings.manager'), $alias));
36
        }
37
    }
38
}
39