Completed
Push — master ( 876946...08c288 )
by Bart
02:55
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Kuleuven\AuthenticationBundle\Service;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
7
8
class ShibbolethAttributesInjectionProviderManager
9
{
10
    /**
11
     * @var AttributeDefinitionsProviderInterface
12
     */
13
    protected $attributeDefinitionsProvider;
14
15
    /**
16
     * @var AttributesInjectionProviderInterface[]|ArrayCollection
17
     */
18
    protected $providerPropertiesCollection;
19
20
    /**
21
     * @param AttributeDefinitionsProviderInterface $attributeDefinitionsProvider
22
     */
23
    public function __construct(AttributeDefinitionsProviderInterface $attributeDefinitionsProvider)
24
    {
25
        $this->attributeDefinitionsProvider = $attributeDefinitionsProvider;
26
        $this->providerPropertiesCollection = new ArrayCollection();
27
    }
28
29
    /**
30
     * @param AttributesInjectionProviderInterface $provider
31
     * @param int                                  $priority
32
     */
33
    public function addProvider(AttributesInjectionProviderInterface $provider, $priority = 0)
34
    {
35
        if ($provider instanceof ParameterAttributesProvider && 0 === $priority) {
36
            $priority = -INF;
37
        }
38
        if ($provider instanceof HeaderAttributesProvider && 0 === $priority) {
39
            $priority = -INF;
40
        }
41
        $this->providerPropertiesCollection->add(['priority' => $priority, 'provider' => $provider]);
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function onKernelRequest(GetResponseEvent $event)
48
    {
49
        if (!$event->isMasterRequest()) {
50
            return;
51
        }
52
53
        if (!empty($this->providerPropertiesCollection)) {
54
            $attributeDefinitions = $this->attributeDefinitionsProvider->getAttributeDefinitions();
55
            $lcIdOrAliasMap = [];
56
            foreach ($attributeDefinitions as $idOrAlias => $attributeDefinition) {
57
                $lcIdOrAliasMap[strtolower($idOrAlias)] = $idOrAlias;
58
            }
59
            $server = $event->getRequest()->server;
60
            $providerPropertiesCollectionIterator = $this->providerPropertiesCollection->getIterator();
61
            $providerPropertiesCollectionIterator->uasort(function ($first, $second) {
62
                // Place highest priority first
63
                if ($first['priority'] === $second['priority']) {
64
                    return 0;
65
                }
66
                return (int)$first['priority'] > (int)$second['priority'] ? -1 : 1;
67
            });
68
            foreach ($providerPropertiesCollectionIterator as $providerProperties) {
69
                /** @var AttributesInjectionProviderInterface $provider */
70
                $provider = $providerProperties['provider'];
71
                if (!$provider->isEnabled()) {
72
                    continue;
73
                }
74
                $attributes = $provider->getAttributes();
75
                if (empty($attributes)) {
76
                    continue;
77
                }
78
                foreach ($attributes as $name => $value) {
79
                    $attributeDefinition = null;
0 ignored issues
show
Unused Code introduced by
$attributeDefinition is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
                    switch (true) {
81
                        case isset($attributeDefinitions[$name]):
82
                            $attributeDefinition = $attributeDefinitions[$name];
83
                            break;
84
                        case isset($lcIdOrAliasMap[$name], $attributeDefinitions[$lcIdOrAliasMap[$name]]):
85
                            $attributeDefinition = $attributeDefinitions[$lcIdOrAliasMap[$name]];
86
                            break;
87
                        default:
88
                            continue 2; // switch is considered a looping structure, we have to continue the foreach
89
                    }
90
                    $id = $attributeDefinition['id'];
91
                    $aliases = $attributeDefinition['aliases'];
92
                    $server->set($id, (string)$value);
93
                    foreach ($aliases as $alias) {
94
                        $server->set($alias, (string)$value);
95
                    }
96
                }
97
            }
98
        }
99
    }
100
}
101