Completed
Push — dev-master ( 97137e...dc8dfb )
by Karol
04:55 queued 01:50
created

Configuration::supportsHashids()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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
21 8
    public function getConfigTreeBuilder(): TreeBuilder
22
    {
23 8
        $treeBuilder = new TreeBuilder(self::ROOT_NAME);
24
        /** @var NodeDefinition|ArrayNodeDefinition $rootNode */
25 8
        $rootNode = RootNodeFactory::create($treeBuilder, self::ROOT_NAME);
26
27
        $rootNode
28 8
            ->children()
29 8
                ->arrayNode(self::NODE_CONVERTER)->addDefaultsIfNotSet()->ignoreExtraKeys(false)
30 8
                    ->children()
31 8
                        ->append($this->addHashidsConverterNode())
32 8
                    ->end()
33 8
                ->end()
34 8
            ->end();
35
36 8
        return $treeBuilder;
37
    }
38
39 8
    private function addHashidsConverterNode(): ArrayNodeDefinition
40
    {
41 8
        $node = new ArrayNodeDefinition(self::NODE_CONVERTER_HASHIDS);
42
43 8
        if (!$this->supportsHashids()) {
44 2
            return $node;
45
        }
46
47
        return $node
48 6
            ->addDefaultsIfNotSet()
49 6
                ->children()
50 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_SALT)
51 6
                        ->defaultNull()
52 6
                    ->end()
53
                    /* @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 6
                    ->end()
57 6
                    ->scalarNode(self::NODE_CONVERTER_HASHIDS_ALPHABET)
58 6
                        ->defaultValue('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890')
59 6
                    ->end()
60 6
                ->end();
61
    }
62
63 6
    public function supportsHashids()
64
    {
65 6
        return class_exists(Hashids::class);
66
    }
67
}
68