Passed
Pull Request — master (#1)
by Vladimir
02:30
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 41
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 45
rs 9.264
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(\Closure::fromCallable([$this, 'hasEnabledIntegrations']))
50
                            ->thenInvalid('At least one integration must be enabled')
51
                        ->end()
52
                        ->validate()
53
                            ->ifTrue(\Closure::fromCallable([$this, 'hasCorrectlyEnabledIntegrations']))
54
                            ->thenInvalid('Enabled integrations must have configured path')
55
                        ->end()
56
                    ->end()
57
                ->end();
58
59
        return $treeBuilder;
60
    }
61
62
    /**
63
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
64
     *
65
     * @param array $integrations
66
     *
67
     * @return bool
68
     */
69
    private function hasEnabledIntegrations(array $integrations): bool
70
    {
71
        /** @var array $integration */
72
        foreach ($integrations as $integration) {
73
            /** @psalm-suppress MixedArrayAccess */
74
            if ($integration['enabled']) {
75
                return false;
76
            }
77
        }
78
79
        return true;
80
    }
81
82
    /**
83
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
84
     *
85
     * @param array $integrations
86
     *
87
     * @return bool
88
     */
89
    private function hasCorrectlyEnabledIntegrations(array $integrations): bool
90
    {
91
        /** @var array $integration */
92
        foreach ($integrations as $integration) {
93
            /** @psalm-suppress MixedArrayAccess */
94
            if ($integration['enabled'] && empty($integration['path'])) {
95
                return true;
96
            }
97
        }
98
99
        return false;
100
    }
101
}
102