Completed
Pull Request — develop (#619)
by
unknown
04:25
created

AuthenticationKeyFinderPass   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 66
ccs 27
cts 31
cp 0.871
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 54 7
1
<?php
2
/**
3
 * Class AuthenticationKeyFinderPass
4
 */
5
6
namespace Graviton\SecurityBundle\DependencyInjection\Compiler;
7
8
use Graviton\SecurityBundle\Authentication\Strategies\MultiStrategy;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Reference;
12
13
/**
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class AuthenticationKeyFinderPass implements CompilerPassInterface
19
{
20
    /**
21
     * You can modify the container here before it is dumped to PHP code.
22
     *
23
     * @api
24
     *
25
     * @param ContainerBuilder $container container to look for tags in
26
     *
27
     * @return void
28
     */
29 4
    public function process(ContainerBuilder $container)
30
    {
31
32
        // Check which Strategy is loaded.
33 4
        if (!$container->hasParameter('graviton.security.authentication.strategy')) {
34
            return;
35
        }
36
37
        // Check which Strategy is used.
38
        $authService
39 4
            = $container->getParameter('graviton.security.authentication.strategy');
40
41
        // Only load multi services if configured.
42 4
        if (strpos($authService, '.multi') === false) {
43 2
            return;
44
        }
45
46
        // Multi Strategy Authentication injection
47
        $serviceIds
48 2
            = $container->getParameter('graviton.security.authentication.strategy.multi.services');
49
50
        // Multi service class
51 2
        $multiAuth = $container->findDefinition('graviton.security.authentication.strategy.multi');
52
53
        // If no service defined we use them all
54 2
        if (empty($serviceIds)) {
55
            $services = $container->findTaggedServiceIds('graviton.security.authenticationkey.finder');
56
            $serviceIds = array_keys($services);
57
        }
58
59
        // Fetch service and add them to Multi
60 2
        foreach ($serviceIds as $id) {
61 2
            if ($container->hasDefinition($id)) {
62 2
                $multiAuth->addMethodCall(
63 2
                    'addStrategy',
64
                    array(
65 2
                        new Reference($id)
66 1
                    )
67 1
                );
68 1
            } else {
69
                /** @var \Psr\Log\LoggerInterface $logger*/
70 2
                if ($container->hasDefinition('logger')) {
71
                    /** @var \Psr\Log\LoggerInterface $logger */
72 2
                    $logger = $container->getDefinition('logger');
73 2
                    $logger->warning(
74 2
                        sprintf(
75 2
                            'The service (%s) is not registered in the application kernel.',
76 1
                            $id
77 1
                        )
78 1
                    );
79 1
                }
80
            }
81 1
        }
82 2
    }
83
}
84