Processor::processConfiguration()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\ConfigCache\Definition;
13
14
use Symfony\Component\Config\Definition\ArrayNode;
15
use Symfony\Component\Config\Definition\ConfigurationInterface;
16
17
/**
18
 * This class is the entry point for config normalization/merging/finalization.
19
 *
20
 * No extends Symfony Processor because of executing validation on each file configuration.
21
 */
22
class Processor
23
{
24
    /**
25
     * Processes an array of configurations.
26
     *
27
     * @param array     $validated  merged and validated array
28
     * @param array     $validating merging and validating array
29
     * @param ArrayNode $node       merging node
30
     * @param ArrayNode $masterNode merged node (master node)
31
     *
32
     * @return array list of (validated array, merged node)
33
     */
34 45
    public function process(
35
        array     $validated,
36
        array     $validating,
37
        ArrayNode $node,
38
        ArrayNode $masterNode = null
39
    ) {
40
        // no setting master node
41 45
        if (is_null($masterNode)) {
42
            // set a node to master node
43 26
            $masterNode = $node;
44
45
        // has setting
46 26
        } else {
47
            // merge a node to master node
48
            // enabled master node setting when exists the same key validation
49
            // check existence for avoid exception trying to set a key that already set
50 25
            $childrenAll = $masterNode->getChildren();
51 25
            foreach ($node->getChildren() as $name => $child) {
52 23
                if (!isset($childrenAll[$name])) {
53 1
                    $masterNode->addChild($child);
54 1
                }
55 25
            }
56
        }
57
58
        // validate root node name, target is merging/validating array
59 45
        foreach ($validating as $name => $config) {
60 45
            if ($masterNode->getName() !== $name || $node->getName() !== $name) {
61 4
                throw new \Exception(
62 4
                    sprintf(
63 4
                        'Settings root[%s] is different from Configuration root[%s] or part[%s].',
64 4
                        $name,
65 4
                        $masterNode->getName(),
66 4
                        $node->getName()
67 4
                    )
68 4
                );
69
            }
70 41
        }
71
72
        // directly set validated array without normalize/merge/finalize
73 41
        $currentConfig = $validated;
74
75
        // loop a validating array
76 41
        foreach ($validating as $config) {
77
            // execute a node's normalize to validate key
78 41
            $config        = $node->normalize($config);
79
            // execute a master node's merge to reflect cannotBeOverwritten key setting and so on
80 38
            $currentConfig = $masterNode->merge($currentConfig, $config);
81 36
        }
82
83
        // execute a master node's finalize
84 36
        $finalized = $masterNode->finalize($currentConfig);
85
86 35
        return array($finalized, $masterNode);
87
    }
88
89
    /**
90
     * Processes an array of configurations.
91
     *
92
     * @param array                  $validated     merged/validated array
93
     * @param array                  $validating    merging/validating array
94
     * @param ConfigurationInterface $configuration configuration
95
     * @param ArrayNode              $masterNode    master node
96
     *
97
     * @return array list of (validated array, merged ArrayNode)
98
     */
99 29
    public function processConfiguration(
100
        array $validated,
101
        array $validating,
102
        ConfigurationInterface $configuration,
103
        ArrayNode $masterNode = null
104
    ) {
105 29
        $node = $configuration->getConfigTreeBuilder()->buildTree();
106
        // skip if the node is not an ArrayNode (normally never happen)
107 29
        if (!($node instanceof ArrayNode)) {
108 1
            return array($validated, $masterNode);
109
        }
110
111 28
        return $this->process($validated, $validating, $node, $masterNode);
112
    }
113
}
114