Completed
Push — master ( aca6e3...e7aa24 )
by Vladimir
05:21
created

CheckManager   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
eloc 45
dl 0
loc 126
c 0
b 0
f 0
ccs 54
cts 54
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A addTag() 0 3 2
A addGroup() 0 3 2
A setTagsMap() 0 4 2
A init() 0 4 1
A setCheckServiceMap() 0 25 5
A findGroups() 0 11 3
A findTags() 0 11 3
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 39
    public function init(array $tagsMap, array $checkServiceMap)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function init()
Loading history...
35
    {
36 39
        $this->setTagsMap($tagsMap);
37 39
        $this->setCheckServiceMap($checkServiceMap);
38 39
    }
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @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...
42
     * @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...
43
     * @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...
44
     *
45
     * @return Tag[]
46
     */
47 2
    public function findChecks($alias = null, $groups = null, $tags = null): array
48
    {
49 2
        $alias = (array) (null === $alias ? [] : (\is_string($alias) ? [$alias] : $alias));
50 2
        $groups = (array) (null === $groups ? [] : (\is_string($groups) ? [$groups] : $groups));
51 2
        $tags = (array) (null === $tags ? [] : (\is_string($tags) ? [$tags] : $tags));
52
53 2
        $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...
54 1
            $inAlias = ($alias) ? \in_array($c->getId(), $alias, true) : true;
55 1
            $inGroups = ($groups) ? \in_array($c->getGroup(), $groups, true) : true;
56 1
            $inTags = ($tags) ? (bool) array_intersect($c->getTags(), $tags) : true;
57
58 1
            return $inAlias && $inGroups && $inTags;
59 2
        });
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...
60
61 2
        return $check;
62
    }
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     *
67
     * @return Group[]
68
     */
69 2
    public function findGroups($groups = null): array
70
    {
71 2
        if ($groups) {
72 2
            $groups = \is_string($groups) ? [$groups] : $groups;
73
74 2
            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...
75 2
                return \in_array($t->getName(), $groups, true);
76 2
            });
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...
77
        }
78
79 1
        return $this->groups;
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @param ?string|string[] $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     *
85
     * @return Group[]
86
     */
87 3
    public function findTags($tags = null): array
88
    {
89 3
        if ($tags) {
90 2
            $tags = \is_string($tags) ? [$tags] : $tags;
91
92 2
            return array_filter($this->tags, static function ($t) use ($tags) {
93 2
                return \in_array($t->getName(), $tags, true);
94 2
            });
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...
95
        }
96
97 2
        return $this->tags;
98
    }
99
100 34
    public function addGroup(Group $group): Group
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addGroup()
Loading history...
101
    {
102 34
        return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()];
103
    }
104
105 5
    public function addTag(Tag $tag): Tag
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addTag()
Loading history...
106
    {
107 5
        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...
108
    }
109
110 39
    protected function setTagsMap(array $tagsMap)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setTagsMap()
Loading history...
111
    {
112 39
        foreach ($tagsMap as $tag => $tagSetting) {
113 2
            $this->addTag(new Tag($tag));
114
        }
115 39
    }
116
117
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
118
     * @param array $checkServiceMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
119
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
120 39
    protected function setCheckServiceMap($checkServiceMap)
121
    {
122 39
        foreach ($checkServiceMap as $checkId => $check) {
123 34
            $checkServiceId = $check['serviceId'];
124 34
            $checkProxy = new Proxy(function () use ($checkServiceId, $checkId) {
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...
125 34
                $this->checks[$checkId] = $this->container->get($checkServiceId);
126
127 34
                return $this->checks[$checkId];
128 34
            });
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...
129
130 34
            $this->checks[$checkId] = $checkProxy;
131
132 34
            foreach ($check['tags'] as $tagName) {
133 4
                $tag = $this->addTag(new Tag($tagName));
134 4
                $tag->addCheck($checkId, $this->checks[$checkId]);
135
            }
136
137 34
            $group = $this->addGroup(new Group($check['group']));
138 34
            $group->addCheck($checkId, $this->checks[$checkId]);
139 34
            $this->groups[$group->getName()] = $group;
140
        }
141
142 39
        foreach ($this->tags as $id => $tag) {
143 5
            if (!\count($tag)) {
144 5
                unset($this->tags[$id]);
145
            }
146
        }
147 39
    }
148
}
149