Configuration::addHashidsConverterNode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 16
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 22
ccs 16
cts 16
cp 1
crap 2
rs 9.7333
1
<?php
2
3
namespace Pgs\HashIdBundle\DependencyInjection;
4
5
use Hashids\Hashids;
6
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
class Configuration implements ConfigurationInterface
11
{
12
    const ROOT_NAME = 'pgs_hash_id';
13
14
    const NODE_CONVERTER = 'converter';
15
    const NODE_CONVERTER_HASHIDS = 'hashids';
16
    const NODE_CONVERTER_HASHIDS_SALT = 'salt';
17
    const NODE_CONVERTER_HASHIDS_MIN_HASH_LENGTH = 'min_hash_length';
18
    const NODE_CONVERTER_HASHIDS_ALPHABET = 'alphabet';
19
20 8
    public function getConfigTreeBuilder(): TreeBuilder
21
    {
22 8
        $treeBuilder = new TreeBuilder(self::ROOT_NAME);
23 8
        $rootNode = $treeBuilder->getRootNode();
24
25
        $rootNode
26 8
            ->children()
27 8
                ->arrayNode(self::NODE_CONVERTER)->addDefaultsIfNotSet()->ignoreExtraKeys(false)
28 8
                    ->children()
29 8
                        ->append($this->addHashidsConverterNode())
30 8
                    ->end()
31 8
                ->end()
32 8
            ->end();
33
34 8
        return $treeBuilder;
35
    }
36
37 8
    private function addHashidsConverterNode(): ArrayNodeDefinition
38
    {
39 8
        $node = new ArrayNodeDefinition(self::NODE_CONVERTER_HASHIDS);
40
41 8
        if (!$this->supportsHashids()) {
42 2
            return $node;
43
        }
44
45
        return $node
46 6
            ->addDefaultsIfNotSet()
47 6
                ->children()
48 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_SALT)
49 6
                        ->defaultNull()
50 6
                    ->end()
51
                    /* @scrutinizer ignore-call */
52 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_MIN_HASH_LENGTH)
0 ignored issues
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                    ->/** @scrutinizer ignore-call */ scalarNode(self::NODE_CONVERTER_HASHIDS_MIN_HASH_LENGTH)
Loading history...
53 6
                        ->defaultValue(10)
54 6
                    ->end()
55 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_ALPHABET)
56 6
                        ->defaultValue('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
57 6
                    ->end()
58 6
                ->end();
59
    }
60
61 6
    public function supportsHashids()
62
    {
63 6
        return class_exists(Hashids::class);
64
    }
65
}
66