Passed
Pull Request — master (#1)
by Vladimir
07:53
created

Configuration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 51
c 1
b 0
f 0
dl 0
loc 74
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 66 6
1
<?php
2
3
namespace Bicycle\TesseractBridgeBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition;
6
7
class Configuration implements Definition\ConfigurationInterface
8
{
9
    /**
10
     * @psalm-suppress PossiblyUndefinedMethod
11
     * @psalm-suppress MixedMethodCall
12
     *
13
     * @return Definition\Builder\TreeBuilder
14
     */
15
    public function getConfigTreeBuilder()
16
    {
17
        $treeBuilder = new Definition\Builder\TreeBuilder('bicycle_tesseract_bridge');
18
19
        $treeBuilder
20
            ->getRootNode()
21
                ->children()
22
                    ->arrayNode('integrations')
23
                        ->isRequired()
24
                        ->children()
25
                            ->arrayNode('cli')
26
                                ->canBeEnabled()
27
                                ->children()
28
                                    ->scalarNode('path')
29
                                        ->defaultNull()
30
                                        ->example('tesseract')
31
                                        ->info('Path to tesseract cli binary. ' .
32
                                            'It could be just "tesseract" in case binary in PATH or relative/absolute')
33
                                    ->end()
34
                                ->end()
35
                            ->end()
36
                            ->arrayNode('ffi')
37
                                ->canBeEnabled()
38
                                ->children()
39
                                    ->scalarNode('path')
40
                                        ->defaultNull()
41
                                        ->example('libtesseract.so.4')
42
                                        ->info('Path to tesseract shared library. It could be' .
43
                                            'just "libtesseract.so.4" in case library is in PATH or relative/absolute')
44
                                    ->end()
45
                                ->end()
46
                            ->end()
47
                        ->end()
48
                        ->validate()
49
                            ->ifTrue(static function (array $integrations) {
50
                                $enabledIntegrationsCount = 0;
51
                                /** @var array $integration */
52
                                foreach ($integrations as $integration) {
53
                                    /** @psalm-suppress MixedArrayAccess */
54
                                    if ($integration['enabled']) {
55
                                        ++$enabledIntegrationsCount;
56
                                    }
57
                                }
58
59
                                return 0 === $enabledIntegrationsCount;
60
                            })
61
                            ->thenInvalid('At least one integration must be enabled')
62
                        ->end()
63
                        ->validate()
64
                            ->ifTrue(static function (array $integrations) {
65
                                /** @var array $integration */
66
                                foreach ($integrations as $integration) {
67
                                    /** @psalm-suppress MixedArrayAccess */
68
                                    if ($integration['enabled'] && empty($integration['path'])) {
69
                                        return true;
70
                                    }
71
                                }
72
73
                                return false;
74
                            })
75
                            ->thenInvalid('Enabled integrations must have configured path')
76
                        ->end()
77
                    ->end()
78
                ->end();
79
80
        return $treeBuilder;
81
    }
82
}
83