Completed
Push — master ( a1429b...fe43f0 )
by Vladimir
06:19
created

ApiController::checkStatusAction()   B

Complexity

Conditions 6
Paths 54

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.031

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 35
ccs 19
cts 21
cp 0.9048
rs 8.9617
c 0
b 0
f 0
cc 6
nc 54
nop 2
crap 6.031
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
146
     * @return array [$checks, $groups, $tags],
147
     */
148 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...
149
    {
150 28
        $checks = $request->get('check', []);
151 28
        if (\is_scalar($checks)) {
152 2
            $checks = $checks ? [$checks] : [];
153
        }
154 28
        $checks = !\is_array($checks) ? [$checks] : $checks;
155
156 28
        $groups = $request->get('group', []);
157 28
        if (\is_scalar($groups)) {
158 8
            $groups = $groups ? [$groups] : [];
159
        }
160 28
        $groups = !\is_array($groups) ? [$groups] : $groups;
161
162 28
        $tags = $request->get('tag', []);
163 28
        if (\is_scalar($tags)) {
164 4
            $tags = $tags ? [$tags] : [];
165
        }
166 28
        $tags = !\is_array($tags) ? [$tags] : $tags;
167
168 28
        return [$checks, $groups, $tags];
169
    }
170
}
171