CheckManager::add()   A
last analyzed

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 3
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\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
    /**
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 $ids
Loading history...
36
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     * @param null|mixed       $ids
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $ids does not match actual variable name $tags
Loading history...
38
     *
39
     * @return Tag[]
40
     */
41 74
    public function findChecks($ids = null, $groups = null, $tags = null): array
42
    {
43 74
        $ids = (array) (null === $ids ? [] : (\is_string($ids) ? [$ids] : $ids));
44 74
        $groups = (array) (null === $groups ? [] : (\is_string($groups) ? [$groups] : $groups));
45 74
        $tags = (array) (null === $tags ? [] : (\is_string($tags) ? [$tags] : $tags));
46
47 74
        $check = array_filter($this->toArray(), static function (CheckInterface $c) use ($ids, $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 73
            $inIds = ($ids) ? \in_array($c->getId(), $ids, true) : true;
49 73
            $inGroups = ($groups) ? \in_array($c->getGroup(), $groups, true) : true;
50 73
            $inTags = ($tags) ? (bool) array_intersect($c->getTags(), $tags) : true;
51
52 73
            return $inIds && $inGroups && $inTags;
53 74
        });
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
55 74
        return $check;
56
    }
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     *
61
     * @return Group[]
62
     */
63 12
    public function findGroups($groups = null): array
64
    {
65 12
        if ($groups) {
66 10
            $groups = \is_string($groups) ? [$groups] : $groups;
67
68 10
            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 10
                return \in_array($t->getName(), $groups, true);
70 10
            });
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
        }
72
73 3
        return $this->groups;
74
    }
75
76
    /**
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
     * @return Group[]
80
     */
81 15
    public function findTags($tags = null): array
82
    {
83 15
        if ($tags) {
84 12
            $tags = \is_string($tags) ? [$tags] : $tags;
85
86 12
            return array_filter($this->tags, static function ($t) use ($tags) {
87 12
                return \in_array($t->getName(), $tags, true);
88 12
            });
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
        }
90
91 5
        return $this->tags;
92
    }
93
94 137
    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 137
        $this->addTags($tagConfs);
97 137
        $this->addGroups($groupConfs);
98 137
        $this->addChecks($checkConfs);
99 137
    }
100
101 99
    public function addIfTag(Tag $tag): Tag
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addIfTag()
Loading history...
102
    {
103 99
        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
106 132
    public function addIfGroup(Group $group): Group
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addIfGroup()
Loading history...
107
    {
108 132
        return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()];
109
    }
110
111
    public function addIfCheck(CheckInterface $check)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addIfCheck()
Loading history...
112
    {
113
        $this->checks[$check->getId()] = $check;
114
115
        foreach ($check->getTags() as $tag) {
116
            $tag = $this->addIfTag(new Tag($tag));
117
            $tag->addCheck($check->getId(), $this->checks[$check->getId()]);
118
        }
119
120
        $group = $this->addIfGroup(new Group($check->getGroup()));
121
        $group->addCheck($check->getId(), $this->checks[$check->getId()]);
122
123
        $this->groups[$group->getId()] = $group;
124
    }
125
126 137
    protected function addTags(array $tagConfs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addTags()
Loading history...
127
    {
128 137
        foreach ($tagConfs as $id => $setting) {
129 3
            $tag = new Tag($id, !empty($setting['name']) ? $setting['name'] : null, !empty($setting['descr']) ? $setting['descr'] : null);
130 3
            $this->addIfTag($tag);
131
        }
132 137
    }
133
134 137
    protected function addGroups(array $gropConfs)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addGroups()
Loading history...
135
    {
136 137
        foreach ($gropConfs as $id => $setting) {
137 1
            $group = new Group($id, !empty($setting['name']) ? $setting['name'] : null, !empty($setting['descr']) ? $setting['descr'] : null);
138 1
            $this->addIfGroup($group);
139
        }
140 137
    }
141
142
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
143
     * @param array $checkConfs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
144
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
145 137
    protected function addChecks($checkConfs)
146
    {
147 137
        foreach ($checkConfs as $id => $setting) {
148 132
            $serviceId = $setting['serviceId'];
149 132
            $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...
150 113
                $this->checks[$id] = $this->container->get($serviceId);
151
152 113
                return $this->checks[$id];
153 132
            });
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...
154
155 132
            $this->checks[$id] = $checkProxy;
156
157 132
            foreach ($setting['tags'] as $tagId) {
158 98
                $tag = $this->addIfTag(new Tag($tagId));
159 98
                $tag->addCheck($id, $this->checks[$id]);
160
            }
161
162 132
            $group = $this->addIfGroup(new Group($setting['group']));
163 132
            $group->addCheck($id, $this->checks[$id]);
164
165 132
            $this->groups[$group->getId()] = $group;
166
        }
167 137
    }
168
}
169