Configuration::getConfigTreeBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paysera\Bundle\ApiBundle\DependencyInjection;
5
6
use Paysera\Bundle\ApiBundle\Entity\PagedQuery;
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13 101
    public function getConfigTreeBuilder()
14
    {
15 101
        $treeBuilder = new TreeBuilder();
16 101
        $rootNode = $treeBuilder->root('paysera_api');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
17
18 101
        $children = $rootNode->children();
19 101
        $children->arrayNode('locales')->defaultValue([])->prototype('scalar');
20
21
        /** @var ArrayNodeDefinition $findDenormalizersNode */
22
        $findDenormalizersNode = $children
23 101
            ->arrayNode('path_attribute_resolvers')
24 101
            ->defaultValue([])
25 101
            ->useAttributeAsKey('class')
26 101
            ->prototype('array')
27
        ;
28 101
        $denormalizerPrototype = $findDenormalizersNode->children();
29 101
        $denormalizerPrototype->scalarNode('field')->defaultValue('id');
30
31 101
        $validationNode = $children->arrayNode('validation')->addDefaultsIfNotSet()->children();
32 101
        $validationNode->scalarNode('property_path_converter')->defaultNull();
33
34 101
        $this->configurePagination($children->arrayNode('pagination'));
35
36 101
        return $treeBuilder;
37
    }
38
39 101
    private function configurePagination(ArrayNodeDefinition $paginationArrayNode)
40
    {
41 101
        $paginationNode = $paginationArrayNode->addDefaultsIfNotSet()->children();
42
        $availableStrategies = [
43 101
            PagedQuery::TOTAL_COUNT_STRATEGY_ALWAYS,
44
            PagedQuery::TOTAL_COUNT_STRATEGY_OPTIONAL,
45
            PagedQuery::TOTAL_COUNT_STRATEGY_NEVER,
46
        ];
47
        $strategyNode = $paginationNode
48 101
            ->scalarNode('total_count_strategy')
49 101
            ->defaultValue(PagedQuery::TOTAL_COUNT_STRATEGY_OPTIONAL)
50
        ;
51 101
        $strategyNode->validate()
52 101
            ->ifNotInArray($availableStrategies)
53 101
            ->thenInvalid(sprintf(
54 101
                'must be one of %s',
55 101
                implode(', ', $availableStrategies)
56
            ))
57
        ;
58
59 101
        $paginationNode->scalarNode('maximum_offset')->defaultValue(1000);
60
        $paginationNode
61 101
            ->scalarNode('maximum_limit')
62 101
            ->defaultValue(1000)
63 101
            ->validate()
64 101
            ->ifTrue(function ($value) {
65 7
                return !is_int($value) || $value <= 0;
66 101
            })
67 101
            ->thenInvalid('must be positive integer')
68
        ;
69 101
        $paginationNode->scalarNode('default_limit')->defaultValue(100);
70
71 101
        $paginationArrayNode->validate()
72 101
            ->ifTrue(function ($value) {
73 6
                return $value['default_limit'] > $value['maximum_limit'];
74 101
            })
75 101
            ->thenInvalid('default_limit cannot be greater than maximum_limit')
76
        ;
77 101
    }
78
}
79