Completed
Push — dev-master ( 911a2b...ffe40d )
by Karol
03:14
created

Configuration::supportsHashids()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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\NodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    const ROOT_NAME = 'pgs_hash_id';
14
15
    const NODE_CONVERTER = 'converter';
16
    const NODE_CONVERTER_HASHIDS = 'hashids';
17
    const NODE_CONVERTER_HASHIDS_SALT = 'salt';
18
    const NODE_CONVERTER_HASHIDS_MIN_HASH_LENGTH = 'min_hash_length';
19
    const NODE_CONVERTER_HASHIDS_ALPHABET = 'alphabet';
20 6
21
    public function getConfigTreeBuilder(): TreeBuilder
22 6
    {
23 6
        $treeBuilder = new TreeBuilder(self::ROOT_NAME);
24 6
        /** @var NodeDefinition|ArrayNodeDefinition $rootNode */
25
        $rootNode = RootNodeFactory::create($treeBuilder, self::ROOT_NAME);
26
27
        $rootNode
28
            ->children()
29
                ->arrayNode(self::NODE_CONVERTER)->addDefaultsIfNotSet()->ignoreExtraKeys(false)
30
                    ->children()
31 6
                        ->append($this->addHashidsConverterNode())
32 6
                    ->end()
33 6
                ->end()
34 6
            ->end();
35 6
36 6
        return $treeBuilder;
37 6
    }
38
39 6
    private function addHashidsConverterNode(): ArrayNodeDefinition
40
    {
41
        $node = new ArrayNodeDefinition(self::NODE_CONVERTER_HASHIDS);
42 6
43
        if (!$this->supportsHashids()) {
44 6
            return $node;
45
        }
46 6
47
        return $node
48
            ->addDefaultsIfNotSet()
49
                ->children()
50
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_SALT)
51 6
                        ->defaultNull()
52 6
                    ->end()
53 6
                    /* @scrutinizer ignore-call */
54 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

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