Completed
Push — dev-master ( 983c82...911a2b )
by Karol
03:04
created

Configuration::doesHashidsExist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 6
    public function getConfigTreeBuilder(): TreeBuilder
21
    {
22 6
        $treeBuilder = new TreeBuilder(self::ROOT_NAME);
23 6
        if (method_exists($treeBuilder, 'getRootNode')) {
24 6
            $rootNode = $treeBuilder->getRootNode();
25
        } else {
26
            // BC layer for symfony/config 4.1 and older
27
            $rootNode = /* @scrutinizer ignore-deprecated */ $treeBuilder->root(self::ROOT_NAME);
28
        }
29
30
        $rootNode
31 6
            ->children()
32 6
                ->arrayNode(self::NODE_CONVERTER)->addDefaultsIfNotSet()->ignoreExtraKeys(false)
33 6
                    ->children()
34 6
                        ->append($this->addHashidsConverterNode())
35 6
                    ->end()
36 6
                ->end()
37 6
            ->end();
38
39 6
        return $treeBuilder;
40
    }
41
42 6
    private function addHashidsConverterNode(): ArrayNodeDefinition
43
    {
44 6
        $node = new ArrayNodeDefinition(self::NODE_CONVERTER_HASHIDS);
45
46 6
        if (!$this->doesHashidsExist()) {
47
            return $node;
48
        }
49
50
        return $node
51 6
            ->addDefaultsIfNotSet()
52 6
                ->children()
53 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_SALT)
54 6
                        ->defaultNull()
55 6
                    ->end()
56
                    /* @scrutinizer ignore-call */
57 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

57
                    ->/** @scrutinizer ignore-call */ scalarNode(self::NODE_CONVERTER_HASHIDS_MIN_HASH_LENGTH)
Loading history...
58 6
                        ->defaultValue(10)
59 6
                    ->end()
60 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_ALPHABET)
61 6
                        ->defaultValue('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
62 6
                    ->end()
63 6
                ->end();
64
    }
65
66 6
    private function doesHashidsExist()
67
    {
68 6
        return class_exists(Hashids::class);
69
    }
70
}
71