Configuration::addDefaultsNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-bundle package.
5
 *
6
 * (c) Zbigniew Ślązak
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 Zibios\Bundle\WrikeBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
18
/**
19
 * This class contains the configuration information for the bundle.
20
 *
21
 * This information is solely responsible for how the different configuration
22
 * sections are normalized, and merged.
23
 *
24
 * Possible handler types and related configurations (brackets indicate optional params):
25
 *
26
 * - api_url:                                OPTIONAL, DEFAULT 'https://www.wrike.com/api/v3/'
27
 * - permanent_tokens:                       OPTIONAL, DEFAULT []
28
 *      firstTokenName: firstTokenCode
29
 *      secondTokenName: secondTokenCode
30
 */
31
class Configuration implements ConfigurationInterface
32
{
33
    const DEFAULT_NAME = 'default';
34
35
    /**
36
     * Generates the configuration tree builder.
37
     *
38
     * @throws \RuntimeException
39
     * @throws \InvalidArgumentException
40
     *
41
     * @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
42
     */
43 37
    public function getConfigTreeBuilder()
44
    {
45 37
        $treeBuilder = new TreeBuilder();
46 37
        $rootNode = $treeBuilder->root('zibios_wrike');
47
        $rootNode
48 37
            ->beforeNormalization()
49 37
                ->always(
50 37
                    function ($v) {
51 37
                        return (array) $v;
52 37
                    }
53
                )
54 37
            ->end();
55
56 37
        $this->addDefaultsNode($rootNode);
57 37
        $this->addPermanentTokensNode($rootNode);
58
59 37
        return $treeBuilder;
60
    }
61
62
    /**
63
     * @param ArrayNodeDefinition $node
64
     *
65
     * @throws \InvalidArgumentException
66
     * @throws \RuntimeException
67
     */
68 37
    public function addDefaultsNode(ArrayNodeDefinition $node)
69
    {
70
        $node
71 37
            ->children()
72 37
                ->scalarNode('api_url')
73 37
                    ->cannotBeEmpty()
74 37
                ->end()
75 37
            ->end()
76
        ;
77 37
    }
78
79
    /**
80
     * @param ArrayNodeDefinition $node
81
     *
82
     * @throws \InvalidArgumentException
83
     * @throws \RuntimeException
84
     */
85 37
    public function addPermanentTokensNode(ArrayNodeDefinition $node)
86
    {
87
        $node
88 37
            ->children()
89 37
                ->arrayNode('permanent_tokens')
90 37
                    ->validate()
91 37
                        ->ifTrue(function ($v) {
92
                            return
93 11
                                array_key_exists('default_token', $v) &&
94
                                (
95 9
                                    false === array_key_exists('tokens', $v) ||
96
                                    (
97 9
                                        true === array_key_exists('tokens', $v) &&
98 11
                                        false === array_key_exists($v['default_token'], $v['tokens'])
99
                                    )
100
                                );
101 37
                        })
102 37
                        ->thenInvalid('Default token not found in tokens array.')
103 37
                    ->end()
104 37
                    ->children()
105 37
                        ->scalarNode('default_token')
106 37
                            ->cannotBeEmpty()
107 37
                        ->end()
108 37
                    ->end()
109 37
                    ->fixXmlConfig('token')
110 37
                    ->children()
111 37
                        ->arrayNode('tokens')
112 37
                            ->validate()
113 37
                                ->ifTrue(function ($v) {
114
                                    /* @var array $v*/
115 17
                                    foreach ($v as $key => $value) {
116 17
                                        if (is_string($key) && strlen($key) > 0) {
117 11
                                            continue;
118
                                        }
119
120 6
                                        return true;
121
                                    }
122
123 11
                                    return false;
124 37
                                })
125
                                ->thenInvalid('The token name should be none empty string.')
126
                            ->end()
127
                            ->requiresAtLeastOneElement()
128
                            ->useAttributeAsKey('name')
129
                            ->prototype('scalar')
130
                                ->cannotBeEmpty()
131
                            ->end()
132
                        ->end()
133
                    ->end()
134
                ->end()
135
            ->end()
136
        ;
137
    }
138
}
139