Test Failed
Push — master ( 54f39c...6dfcfe )
by Vladimir
04:47
created

ApiController::checkStatusAction()   A

Complexity

Conditions 4
Paths 23

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.3244

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 29
ccs 8
cts 11
cp 0.7272
rs 9.7333
c 0
b 0
f 0
cc 4
nc 23
nop 2
crap 4.3244
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 Tvi\MonitorBundle\Exception\HttpException;
18
use Tvi\MonitorBundle\Reporter\Api;
19
use Tvi\MonitorBundle\Reporter\ReporterManager;
20
use Tvi\MonitorBundle\Runner\RunnerManager;
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @author Vladimir Turnaev <[email protected]>
24
 */
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...
25
class ApiController
26
{
27
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
     * @var RunnerManager
29
     */
30
    protected $runnerManager;
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var ReporterManager
34
     */
35
    protected $reporterManager;
36 1
37
    public function __construct(RunnerManager $runnerManager, ReporterManager $reporterManager)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
38 1
    {
39 1
        $this->runnerManager = $runnerManager;
40 1
        $this->reporterManager = $reporterManager;
41
    }
42 1
43
    public function checkListAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkListAction()
Loading history...
44
    {
45 1
        try {
46
            list($checks, $groups, $tags) = $this->getFilterParams($request);
47 1
48
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
49 1
50 1
            $breakOnFailure = (bool) $request->get('bof', false);
51
            $runner->setBreakOnFailure($breakOnFailure);
52
53 1
            /** @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...
54
            $reporter = $this->reporterManager->getReporter('api');
55 1
56 1
            $runner->addReporter($reporter);
57
            $runner->run();
58 1
59 1
            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...
60 1
                'statusCode' => $reporter->getStatusCode(),
61
                'statusName' => $reporter->getStatusName(),
62 1
63 1
                'successes' => $reporter->getSuccessCount(),
64 1
                'warnings' => $reporter->getWarningCount(),
65 1
                'failures' => $reporter->getFailureCount(),
66 1
                'unknowns' => $reporter->getUnknownCount(),
67
                'total' => $reporter->getTotalCount(),
68 1
69
                'checks' => $reporter->getCheckResults(),
70
            ]);
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
        } catch (\Exception $e) {
72
            $e = new HttpException(404, $e->getMessage());
73
74
            return new JsonResponse($e->toArray(), $e->getStatusCode());
75
        }
76
    }
77
78
    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

78
    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...
79
    {
80
        try {
81
            $runner = $this->runnerManager->getRunner($check);
82
83
            /** @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...
84
            $reporter = $this->reporterManager->getReporter('api');
85
86
            $runner->addReporter($reporter);
87
            $runner->run();
88
89
            $res = $reporter->getCheckResults()[0];
90
91
            return new JsonResponse($res);
92
        } catch (\Exception $e) {
93
            $e = new HttpException(404, $e->getMessage());
94
95
            return new JsonResponse($e->toArray(), $e->getStatusCode());
96
        }
97
    }
98
99
    public function checkStatusAction(Request $request, ?string $check = null): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkStatusAction()
Loading history...
100
    {
101 1
        try {
102
103 1
            list($checks, $groups, $tags) = $this->getFilterParams($request);
104 1
105
            if($check !== null) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
106
                $runner = $this->runnerManager->getRunner($check);
107
            } else {
108 1
                $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
109 1
            }
110
111
            $breakOnFailure = (bool) $request->get('bof', false);
112
            $runner->setBreakOnFailure($breakOnFailure);
113 1
114 1
            /** @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...
115
            $reporter = $this->reporterManager->getReporter('api');
116
117
            $runner->addReporter($reporter);
118 1
            $runner->run();
119
120
            $code = $reporter->getStatusCode() == $reporter::STATUS_CODE_SUCCESS ? 200 : 500;
121
122
            return new Response($reporter->getStatusName(), $code);
123
124
        } catch (\Exception $e) {
125
            $e = new HttpException(404, $e->getMessage());
126
127
            return new JsonResponse($e->toArray(), $e->getStatusCode());
128
        }
129
    }
130
131
    /**
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...
132
     * @return array [$checks, $groups, $tags],
133
     */
134
    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...
135
    {
136
        $checks = $request->get('check', []);
137
        if (\is_scalar($checks)) {
138
            $checks = $checks ? [$checks] : [];
139
        }
140
        $checks = !\is_array($checks) ? [$checks] : $checks;
141
142
        $groups = $request->get('group', []);
143
        if (\is_scalar($groups)) {
144
            $groups = $groups ? [$groups] : [];
145
        }
146
        $groups = !\is_array($groups) ? [$groups] : $groups;
147
148
        $tags = $request->get('tag', []);
149
        if (\is_scalar($tags)) {
150
            $tags = $tags ? [$tags] : [];
151
        }
152
        $tags = !\is_array($tags) ? [$tags] : $tags;
153
154
        return [$checks, $groups, $tags];
155
    }
156
}
157