Completed
Push — ezp28890-fix_in_context_transl... ( c620fe...b89572 )
by
unknown
22:17
created

PoliciesConfigBuilder::addConfig()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 8
nop 1
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the eZ Publish Kernel package.
4
 *
5
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
6
 * @license For full copyright and license information view LICENSE file distributed with this source code.
7
 */
8
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Security\PolicyProvider;
9
10
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ContainerConfigBuilder;
11
use Symfony\Component\Config\Resource\ResourceInterface;
12
13
class PoliciesConfigBuilder extends ContainerConfigBuilder
14
{
15
    public function addConfig(array $config)
16
    {
17
        $previousPolicyMap = [];
18
19
        if ($this->containerBuilder->hasParameter('ezpublish.api.role.policy_map')) {
20
            $previousPolicyMap = $this->containerBuilder->getParameter('ezpublish.api.role.policy_map');
21
        }
22
23
        // We receive limitations as values, but we want them as keys to be used by isset().
24
        foreach ($config as $module => $functionArray) {
25
            foreach ($functionArray as $function => $limitationCollection) {
26
                if (null !== $limitationCollection && $this->policyExists($previousPolicyMap, $module, $function)) {
27
                    $limitations = array_merge_recursive($previousPolicyMap[$module][$function], array_fill_keys((array)$limitationCollection, true));
28
                } else {
29
                    $limitations = array_fill_keys((array)$limitationCollection, true);
30
                }
31
32
                $previousPolicyMap[$module][$function] = $limitations;
33
            }
34
        }
35
36
        $this->containerBuilder->setParameter(
37
            'ezpublish.api.role.policy_map',
38
            $previousPolicyMap
39
        );
40
    }
41
42
    public function addResource(ResourceInterface $resource)
43
    {
44
        $this->containerBuilder->addResource($resource);
45
    }
46
47
    /**
48
     * Checks if policy for module and function exist in Policy Map.
49
     *
50
     * @param array $policyMap
51
     * @param string $module
52
     * @param string $function
53
     *
54
     * @return bool
55
     */
56
    private function policyExists(array $policyMap, $module, $function)
57
    {
58
        return array_key_exists($module, $policyMap) && array_key_exists($function, $policyMap[$module]);
59
    }
60
}
61