Completed
Push — master ( a1dd26...9bde22 )
by Vladimir
07:42
created

ApiController::checkListInfoAction()   A

Complexity

Conditions 3
Paths 11

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 23
ccs 0
cts 15
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 11
nop 1
crap 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\Controller;
13
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Tvi\MonitorBundle\Exception\HttpException;
19
use Tvi\MonitorBundle\Reporter\Api;
20
use Tvi\MonitorBundle\Reporter\ReporterManager;
21
use Tvi\MonitorBundle\Runner\RunnerManager;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author Vladimir Turnaev <[email protected]>
25
 */
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...
26
class ApiController
27
{
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
29
     * @var RunnerManager
30
     */
31
    protected $runnerManager;
32
33
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
     * @var ReporterManager
35
     */
36
    protected $reporterManager;
37
38 46
    public function __construct(RunnerManager $runnerManager, ReporterManager $reporterManager)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
39
    {
40 46
        $this->runnerManager = $runnerManager;
41 46
        $this->reporterManager = $reporterManager;
42 46
    }
43
44 18
    public function checkAction(Request $request, string $check): JsonResponse
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

44
    public function checkAction(/** @scrutinizer ignore-unused */ Request $request, string $check): JsonResponse

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing doc comment for function checkAction()
Loading history...
45
    {
46
        try {
47 18
            $runner = $this->runnerManager->getRunner($check);
48
49
            /** @var $reporter Api */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
50 18
            $reporter = $this->reporterManager->getReporter('api');
51
52 18
            $runner->addReporter($reporter);
53 18
            $runner->run();
54
55 18
            $res = $reporter->getCheckResults();
56
57 18
            if (isset($res[0])) {
58 17
                return new JsonResponse($res[0]);
59
            }
60 1
            throw new NotFoundHttpException(sprintf('Check %s not found', $check));
61 1
        } catch (NotFoundHttpException $e) {
62 1
            $e = new HttpException(404, $e->getMessage());
63
64 1
            return new JsonResponse($e->toArray(), $e->getStatusCode());
65
        } catch (\Exception $e) {
66
            $e = new HttpException(500, $e->getMessage());
67
68
            return new JsonResponse($e->toArray(), $e->getStatusCode());
69
        }
70
    }
71
72 11
    public function checkListAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkListAction()
Loading history...
73
    {
74
        try {
75 11
            list($checks, $groups, $tags) = $this->getFilterParams($request);
76
77 11
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
78
79 11
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
80 11
            $runner->setBreakOnFailure($breakOnFailure);
81
82
            /** @var $reporter Api */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
83 11
            $reporter = $this->reporterManager->getReporter('api');
84
85 11
            $runner->addReporter($reporter);
86 11
            $runner->run();
87
88 11
            return new JsonResponse([
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...
89 11
                'statusCode' => $reporter->getStatusCode(),
90 11
                'statusName' => $reporter->getStatusName(),
91
92 11
                'successes' => $reporter->getSuccessCount(),
93 11
                'warnings' => $reporter->getWarningCount(),
94 11
                'failures' => $reporter->getFailureCount(),
95 11
                'unknowns' => $reporter->getUnknownCount(),
96 11
                'total' => $reporter->getTotalCount(),
97
98 11
                'checks' => $reporter->getCheckResults(),
99
            ]);
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...
100
        } catch (\Exception $e) {
101
            $e = new HttpException(500, $e->getMessage());
102
103
            return new JsonResponse($e->toArray(), $e->getStatusCode());
104
        }
105
    }
106
107 17
    public function checkStatusAction(Request $request, ?string $checkSingle = null): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkStatusAction()
Loading history...
108
    {
109
        try {
110 17
            list($checks, $groups, $tags) = $this->getFilterParams($request);
111
112 17
            $checks = array_filter($checks, static function ($i) {
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...
113 3
                return null !== $i;
114 17
            });
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...
115
116 17
            if (null !== $checkSingle) {
117 1
                $runner = $this->runnerManager->getRunner($checkSingle);
118
            } else {
119 16
                $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
120
            }
121
122 17
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
123 17
            $runner->setBreakOnFailure($breakOnFailure);
124
125
            /** @var $reporter Api */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
126 17
            $reporter = $this->reporterManager->getReporter('api');
127
128 17
            $runner->addReporter($reporter);
129 17
            $runner->run();
130
131 17
            if ($reporter->getTotalCount()) {
132 14
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500;
133
134 14
                return new Response($reporter->getStatusName(), $code);
135
            }
136
137 3
            throw new NotFoundHttpException('Check(s) not found');
138 3
        } catch (NotFoundHttpException $e) {
139 3
            return new Response($e->getMessage(), $e->getStatusCode());
140
        } catch (\Exception $e) {
141
            return new Response($e->getMessage(), 502);
142
        }
143
    }
144
145
    public function checkInfoListAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkInfoListAction()
Loading history...
146
    {
147
        try {
148
            list($checks, $groups, $tags) = $this->getFilterParams($request);
149
150
            $checks = $this->runnerManager->findChecks($checks, $groups, $tags);
151
152
            $data = [];
153
            foreach ($checks as $check) {
154
                $tags = $check->getTags();
155
156
                $d = ['check' => $check->getId(), 'label' => $check->getLabel(), 'group' => $check->getGroup(), 'tags' => $tags, 'Descr' => $check->getDescr()];
157
                $d = array_filter($d, static function ($v) {
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...
158
                    return !empty($v);
159
                });
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...
160
                $data[] = $d;
161
            }
162
163
            return new JsonResponse($data);
164
        } catch (\Exception $e) {
165
            $e = new HttpException(500, $e->getMessage());
166
167
            return new JsonResponse($e->toArray(), $e->getStatusCode());
168
        }
169
    }
170
171
    public function groupInfoListAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function groupInfoListAction()
Loading history...
172
    {
173
        try {
174
            list($checks, $groups, $tags) = $this->getFilterParams($request);
175
            $groups = $this->runnerManager->findGroups($groups);
176
177
            $data = [];
178
            foreach ($groups as $group) {
179
                $d = ['group' => $group->getName(), 'label' => $group->getLabel()];
180
                $data[] = $d;
181
            }
182
183
            return new JsonResponse($data);
184
        } catch (\Exception $e) {
185
            $e = new HttpException(500, $e->getMessage());
186
187
            return new JsonResponse($e->toArray(), $e->getStatusCode());
188
        }
189
    }
190
191
    public function tagInfoListAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tagInfoListAction()
Loading history...
192
    {
193
        try {
194
            try {
195
                list($checks, $groups, $tags) = $this->getFilterParams($request);
196
                $tags = $this->runnerManager->findTags($tags);
197
198
                $data = [];
199
                foreach ($tags as $tag) {
200
                    $d = ['tag' => $tag->getName(), 'label' => $tag->getLabel()];
201
                    $data[] = $d;
202
                }
203
204
                return new JsonResponse($data);
205
            } catch (\Exception $e) {
206
                $e = new HttpException(500, $e->getMessage());
207
208
                return new JsonResponse($e->toArray(), $e->getStatusCode());
209
            }
210
        } catch (\Exception $e) {
211
            $e = new HttpException(500, $e->getMessage());
212
213
            return new JsonResponse($e->toArray(), $e->getStatusCode());
214
        }
215
    }
216
217
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
218
     * return array [$checks, $groups, $tags].
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
219
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
220 28
    private function getFilterParams(Request $request): array
0 ignored issues
show
Coding Style introduced by
Private method name "ApiController::getFilterParams" must be prefixed with an underscore
Loading history...
221
    {
222 28
        $checks = $request->get('check', []);
223 28
        if (\is_scalar($checks)) {
224 2
            $checks = $checks ? [$checks] : [];
225
        }
226 28
        $checks = !\is_array($checks) ? [$checks] : $checks;
227
228 28
        $groups = $request->get('group', []);
229 28
        if (\is_scalar($groups)) {
230 8
            $groups = $groups ? [$groups] : [];
231
        }
232 28
        $groups = !\is_array($groups) ? [$groups] : $groups;
233
234 28
        $tags = $request->get('tag', []);
235 28
        if (\is_scalar($tags)) {
236 4
            $tags = $tags ? [$tags] : [];
237
        }
238 28
        $tags = !\is_array($tags) ? [$tags] : $tags;
239
240 28
        return [$checks, $groups, $tags];
241
    }
242
}
243