Completed
Push — master ( 0fb449...a1dd26 )
by Vladimir
06:31
created

ApiController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\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
    public function checkListInfoAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkListInfoAction()
Loading history...
108
    {
109
        try {
110
            list($checks, $groups, $tags) = $this->getFilterParams($request);
111
112
            $checks = $this->runnerManager->findChecks($checks, $groups, $tags);
113
114
            $data = [];
115
            foreach ($checks as $check) {
116
                $tags = $check->getTags();
117
118
                $d = ['check' => $check->getId(), 'label' => $check->getLabel(), 'Group' => $check->getGroup(), 'tags' => $tags, 'Descr' => $check->getDescr()];
119
                $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...
120
                    return !empty($v);
121
                });
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
                $data[] = $d;
123
            }
124
125
            return new JsonResponse($data);
126
        } catch (\Exception $e) {
127
            $e = new HttpException(500, $e->getMessage());
128
129
            return new JsonResponse($e->toArray(), $e->getStatusCode());
130
        }
131
    }
132
133 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...
134
    {
135
        try {
136 17
            list($checks, $groups, $tags) = $this->getFilterParams($request);
137
138 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...
139 3
                return null !== $i;
140 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...
141
142 17
            if (null !== $checkSingle) {
143 1
                $runner = $this->runnerManager->getRunner($checkSingle);
144
            } else {
145 16
                $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
146
            }
147
148 17
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
149 17
            $runner->setBreakOnFailure($breakOnFailure);
150
151
            /** @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...
152 17
            $reporter = $this->reporterManager->getReporter('api');
153
154 17
            $runner->addReporter($reporter);
155 17
            $runner->run();
156
157 17
            if ($reporter->getTotalCount()) {
158 14
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500;
159
160 14
                return new Response($reporter->getStatusName(), $code);
161
            }
162
163 3
            throw new NotFoundHttpException('Check(s) not found');
164 3
        } catch (NotFoundHttpException $e) {
165 3
            return new Response($e->getMessage(), $e->getStatusCode());
166
        } catch (\Exception $e) {
167
            return new Response($e->getMessage(), 502);
168
        }
169
    }
170
171
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
172
     * 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...
173
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
174 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...
175
    {
176 28
        $checks = $request->get('check', []);
177 28
        if (\is_scalar($checks)) {
178 2
            $checks = $checks ? [$checks] : [];
179
        }
180 28
        $checks = !\is_array($checks) ? [$checks] : $checks;
181
182 28
        $groups = $request->get('group', []);
183 28
        if (\is_scalar($groups)) {
184 8
            $groups = $groups ? [$groups] : [];
185
        }
186 28
        $groups = !\is_array($groups) ? [$groups] : $groups;
187
188 28
        $tags = $request->get('tag', []);
189 28
        if (\is_scalar($tags)) {
190 4
            $tags = $tags ? [$tags] : [];
191
        }
192 28
        $tags = !\is_array($tags) ? [$tags] : $tags;
193
194 28
        return [$checks, $groups, $tags];
195
    }
196
}
197