Test Failed
Push — master ( 3e1f63...b2d2e1 )
by Vladimir
04:38
created

CheckManager   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
eloc 47
dl 0
loc 132
ccs 55
cts 55
cp 1
rs 9.68
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addIfTag() 0 3 2
A addChecks() 0 21 3
A addGroups() 0 5 4
A addIfGroup() 0 3 2
A add() 0 5 1
A findGroups() 0 11 3
A findTags() 0 11 3
A addTags() 0 5 4
C findChecks() 0 15 12
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\Check;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15
16
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
17
 * @author Vladimir Turnaev <[email protected]>
18
 */
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...
19
class CheckManager implements \ArrayAccess, \Iterator, \Countable
20
{
21
    use ContainerAwareTrait;
22
    use CheckArraybleTrait;
23
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var Group[]
26
     */
27
    protected $groups = [];
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var Group[]
31
     */
32
    protected $tags = [];
33
34 88
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @param ?string|string[] $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $tags does not match actual variable name $alias
Loading history...
36 88
     * @param ?string|string[] $alias
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $alias does not match actual variable name $groups
Loading history...
37 88
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $groups does not match actual variable name $tags
Loading history...
38 88
     *
39
     * @return Tag[]
40
     */
41
    public function findChecks($alias = null, $groups = null, $tags = null): array
42
    {
43
        $alias = (array) (null === $alias ? [] : (\is_string($alias) ? [$alias] : $alias));
44
        $groups = (array) (null === $groups ? [] : (\is_string($groups) ? [$groups] : $groups));
45
        $tags = (array) (null === $tags ? [] : (\is_string($tags) ? [$tags] : $tags));
46
47 48
        $check = array_filter($this->toArray(), static function (CheckInterface $c) use ($alias, $groups, $tags) {
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...
48
            $inAlias = ($alias) ? \in_array($c->getId(), $alias, true) : true;
49 48
            $inGroups = ($groups) ? \in_array($c->getGroup(), $groups, true) : true;
50 48
            $inTags = ($tags) ? (bool) array_intersect($c->getTags(), $tags) : true;
51 48
52
            return $inAlias && $inGroups && $inTags;
53 48
        });
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...
54 47
55 47
        return $check;
56 47
    }
57
58 47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59 48
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     *
61 48
     * @return Group[]
62
     */
63
    public function findGroups($groups = null): array
64
    {
65
        if ($groups) {
66
            $groups = \is_string($groups) ? [$groups] : $groups;
67
68
            return array_filter($this->groups, static function ($t) use ($groups) {
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...
69 2
                return \in_array($t->getName(), $groups, true);
70
            });
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...
71 2
        }
72 2
73
        return $this->groups;
74 2
    }
75 2
76 2
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
77
     * @param ?string|string[] $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     *
79 1
     * @return Group[]
80
     */
81
    public function findTags($tags = null): array
82
    {
83
        if ($tags) {
84
            $tags = \is_string($tags) ? [$tags] : $tags;
85
86
            return array_filter($this->tags, static function ($t) use ($tags) {
87 3
                return \in_array($t->getName(), $tags, true);
88
            });
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...
89 3
        }
90 2
91
        return $this->tags;
92 2
    }
93 2
94 2
    public function add(array $checkConfs, array $tagConfs, array $groupConfs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function add()
Loading history...
95
    {
96
        $this->addTags($tagConfs);
97 2
        $this->addGroups($groupConfs);
98
        $this->addChecks($checkConfs);
99
    }
100 83
101
    protected function addIfTag(Tag $tag): Tag
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addIfTag()
Loading history...
102 83
    {
103
        return empty($this->tags[$tag->getName()]) ? $this->tags[$tag->getName()] = $tag : $this->tags[$tag->getName()];
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty($this->tags...->tags[$tag->getName()] could return the type Tvi\MonitorBundle\Check\Group which is incompatible with the type-hinted return Tvi\MonitorBundle\Check\Tag. Consider adding an additional type-check to rule them out.
Loading history...
104
    }
105 53
106
    protected function addTags(array $tagConfs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addTags()
Loading history...
107 53
    {
108
        foreach ($tagConfs as $id => $setting) {
109
            $tag = new Tag($id, !empty($setting['name']) ? $setting['name'] : null, !empty($setting['descr']) ? $setting['descr'] : null);
110 88
            $this->addIfTag($tag);
111
        }
112 88
    }
113 2
114
    protected function addIfGroup(Group $group): Group
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addIfGroup()
Loading history...
115 88
    {
116
        return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()];
117
    }
118
119
    protected function addGroups(array $gropConfs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addGroups()
Loading history...
120 88
    {
121
        foreach ($gropConfs as $id => $setting) {
122 88
            $group = new Group($id, !empty($setting['name']) ? $setting['name'] : null, !empty($setting['descr']) ? $setting['descr'] : null);
123 83
            $this->addIfGroup($group);
124 83
        }
125 83
    }
126
127 83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
128 83
     * @param array $checkConfs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
130 83
    protected function addChecks($checkConfs)
131
    {
132 83
        foreach ($checkConfs as $id => $setting) {
133 52
            $serviceId = $setting['serviceId'];
134 52
            $checkProxy = new Proxy(function () use ($serviceId, $id) {
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...
135
                $this->checks[$id] = $this->container->get($serviceId);
136
137 83
                return $this->checks[$id];
138 83
            });
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...
139 83
140
            $this->checks[$id] = $checkProxy;
141
142 88
            foreach ($setting['tags'] as $tagId) {
143 53
                $tag = $this->addIfTag(new Tag($tagId));
144 53
                $tag->addCheck($id, $this->checks[$id]);
145
            }
146
147 88
            $group = $this->addIfGroup(new Group($setting['group']));
148
            $group->addCheck($id, $this->checks[$id]);
149
150
            $this->groups[$group->getId()] = $group;
151
        }
152
    }
153
}
154