Completed
Push — master ( dd47f4...ab9e9d )
by Vladimir
06:06
created

Configuration::addChecksSearchPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Tvi\MonitorBundle\Check\CheckPluginFinder;
18
19
/**
20
 * This class contains the configuration information for the bundle.
21
 *
22
 * This information is solely responsible for how the different configuration
23
 * sections are normalized, and merged.
24
 *
25
 * @author Vladimir Turnaev <[email protected]>
26
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
27
class Configuration implements ConfigurationInterface
28
{
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var array
31
     */
32
    private $checkPlugins = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "checkPlugins" must be prefixed with an underscore
Loading history...
33
34
    private $checkPluginClasses = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "checkPluginClasses" must be prefixed with an underscore
Loading history...
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pluginFinder should have a doc-comment as per coding-style.
Loading history...
37
     * Configuration constructor.
38
     *
39
     * @param string[]|null $checksSearchPaths
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $checksSearchPaths does not match actual variable name $pluginFinder
Loading history...
40
     */
41 53
    public function __construct(CheckPluginFinder $pluginFinder)
42
    {
43 53
        $this->checkPluginClasses = $pluginFinder->find();
44 53
    }
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
47
     * @return array
48
     */
49 47
    public function getCheckPlugins(): array
50
    {
51 47
        return $this->checkPlugins;
52
    }
53
54
    /**
55
     * Generates the configuration tree.
56
     *
57
     * @return TreeBuilder
58
     */
59 53
    public function getConfigTreeBuilder()
60
    {
61 53
        $treeBuilder = new TreeBuilder();
62
63 53
        $treeBuilder->root('tvi_monitor', 'array')
64 53
            ->children()
65 53
                ->append($this->addChecksSearchPaths())
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 16
Loading history...
66 53
                ->append($this->addTags())
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 16
Loading history...
67 53
                ->append($this->addReporers())
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 16
Loading history...
68 53
                ->append($this->addChecks())
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 16
Loading history...
69 53
            ->end()
70 53
        ->end();
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 8
Loading history...
71
72 53
        return $treeBuilder;
73
    }
74
75 53
    private function addChecks(): ArrayNodeDefinition
0 ignored issues
show
Coding Style introduced by
Private method name "Configuration::addChecks" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function addChecks()
Loading history...
76
    {
77 53
        $builder = new TreeBuilder();
78
79 53
        $checkPligins = $this->checkPlugins;
80
81 53
        $addChecks = function ($rootNode) use ($checkPligins, $builder) {
0 ignored issues
show
Unused Code introduced by
The import $checkPligins is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
82 53
            foreach ($this->checkPluginClasses as $checkPluginClass) {
83 53
                $checkPligin = new $checkPluginClass();
84
85 53
                $confMethods = array_filter(get_class_methods($checkPligin), static function ($n) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
86 53
                    return preg_match('/Conf$/', $n);
87 53
                });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
88
89 53
                foreach ($confMethods as $confMethod) {
90
91
                    /* @var ArrayNodeDefinition $node */
92 53
                    $node = $checkPligin->$confMethod($builder);
93 53
                    $checkName = $node->getNode(true)->getName();
94 53
                    $serviceName = preg_replace('/_factory$/', '', $checkName);
95
96 53
                    $this->checkPlugins[$checkName] = [
97 53
                        'checkServicePath' => $checkPligin::PATH.\DIRECTORY_SEPARATOR.'check.yml',
98 53
                        'service' => $serviceName,
99 53
                        'pligin' => $checkPligin,
100
                    ];
101
102 53
                    $rootNode->append($node);
103
                }
104
            }
105 53
            return $rootNode;
106 53
        };
107
108
        $node = $builder
109 53
            ->root('checks', 'array')
110 53
            ->beforeNormalization()
111 53
            ->always(static function ($value) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
112 42
                $value = $value ? $value : [];
113 42
                foreach ($value as $k => $v) {
114 42
                    $newK = str_replace('(s)', '_factory', $k);
115 42
                    if ($newK !== $k) {
116 37
                        $value[$newK] = $value[$k];
117 42
                        unset($value[$k]);
118
                    }
119
                }
120 42
                return $value;
121 53
            })
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
122 53
            ->end()
123 53
            ->children(); //--
124 53
        $node = $addChecks($node)
125 53
            ->end();
126
127 53
        return $node;
128
    }
129
130 53
    private function addReporers(): ArrayNodeDefinition
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addReporers()
Loading history...
Coding Style introduced by
Private method name "Configuration::addReporers" must be prefixed with an underscore
Loading history...
131
    {
132 53
        return (new TreeBuilder())
133 53
            ->root('reporters', 'array')
134 53
            ->children()
135 53
                ->arrayNode('mailer')
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 16
Loading history...
136 53
                    ->children()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 20
Loading history...
137 53
                        ->scalarNode('recipient')
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
138 53
                            ->isRequired()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
139 53
                            ->cannotBeEmpty()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
140 53
                        ->end()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
141 53
                        ->scalarNode('sender')
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
142 53
                            ->isRequired()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
143 53
                            ->cannotBeEmpty()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
144 53
                        ->end()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
145 53
                        ->scalarNode('subject')
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
146 53
                            ->isRequired()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
147 53
                            ->cannotBeEmpty()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
148 53
                        ->end()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
149 53
                        ->booleanNode('send_on_warning')
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
150 53
                            ->defaultTrue()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 28
Loading history...
151 53
                        ->end()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 24
Loading history...
152 53
                    ->end()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 20
Loading history...
153 53
                ->end()
0 ignored issues
show
Coding Style introduced by
Object operator not indented correctly; expected 12 spaces but found 16
Loading history...
154 53
            ->end();
155
    }
156
157 53
    private function addTags(): ArrayNodeDefinition
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addTags()
Loading history...
Coding Style introduced by
Private method name "Configuration::addTags" must be prefixed with an underscore
Loading history...
158
    {
159 53
        return (new TreeBuilder())
160 53
            ->root('tags', 'array')
161 53
            ->prototype('scalar')->end();
0 ignored issues
show
Bug introduced by
The method prototype() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
            ->/** @scrutinizer ignore-call */ prototype('scalar')->end();
Loading history...
162
    }
163
164 53
    private function addChecksSearchPaths(): ArrayNodeDefinition
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addChecksSearchPaths()
Loading history...
Coding Style introduced by
Private method name "Configuration::addChecksSearchPaths" must be prefixed with an underscore
Loading history...
165
    {
166 53
        return (new TreeBuilder())
167 53
            ->root('checks_search_paths', 'array')
168 53
            ->prototype('scalar')->end();
169
    }
170
}
171