Completed
Push — master ( b086cb...c1a4fd )
by Vladimir
06:51
created

ApiController::checkAction()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

77
    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...
78
    {
79
        try {
80
            $runner = $this->runnerManager->getRunner($check);
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
            $reporter = $this->reporterManager->getReporter('api');
84
85
            $runner->addReporter($reporter);
86
            $runner->run($check);
87
88
            $res = $reporter->getCheckResults()[0];
89
90
            return new JsonResponse($res);
91
        } catch (\Exception $e) {
92
            $e = new HttpException(404, $e->getMessage());
93
94
            return new JsonResponse($e->toArray(), $e->getStatusCode());
95
        }
96
    }
97
98
    /**
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...
99
     * @return array [$checks, $groups, $tags],
100
     */
101 1
    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...
102
    {
103 1
        $checks = $request->get('check', []);
104 1
        if (\is_string($checks)) {
105
            $checks = $checks ? [$checks] : [];
106
        }
107
108 1
        $groups = $request->get('group', []);
109 1
        if (\is_string($groups)) {
110
            $groups = $groups ? [$groups] : [];
111
        }
112
113 1
        $tags = $request->get('tag', []);
114 1
        if (\is_string($tags)) {
115
            $tags = $tags ? [$tags] : [];
116
        }
117
118 1
        return [$checks, $groups, $tags];
119
    }
120
}
121