Test Failed
Push — master ( 3e1f63...b2d2e1 )
by Vladimir
04:38
created

ApiCheckTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 57
dl 0
loc 100
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkAction() 0 25 4
B checkStatusAction() 0 35 6
A checkListAction() 0 32 2
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
 * @property RunnerManager   $runnerManager
25
 * @property ReporterManager $reporterManager
26
 *
27
 * @author Vladimir Turnaev <[email protected]>
28
 */
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...
29
trait ApiCheckTrait
30
{
31
    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

31
    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...
32
    {
33
        try {
34
            $runner = $this->runnerManager->getRunner($check);
35
36
            /** @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...
37
            $reporter = $this->reporterManager->getReporter('api');
38
39
            $runner->addReporter($reporter);
40
            $runner->run();
41
42
            $res = $reporter->getCheckResults();
43
44
            if (isset($res[0])) {
45
                return new JsonResponse($res[0]);
46
            }
47
            throw new NotFoundHttpException(sprintf('Check %s not found', $check));
48
        } catch (NotFoundHttpException $e) {
49
            $e = new HttpException(404, $e->getMessage());
50
51
            return new JsonResponse($e->toArray(), $e->getStatusCode());
52
        } catch (\Exception $e) {
53
            $e = new HttpException(500, $e->getMessage());
54
55
            return new JsonResponse($e->toArray(), $e->getStatusCode());
56
        }
57
    }
58
59
    public function checkListAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkListAction()
Loading history...
60
    {
61
        try {
62
            list($checks, $groups, $tags) = $this->getFilterParams($request);
0 ignored issues
show
Bug introduced by
It seems like getFilterParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
            /** @scrutinizer ignore-call */ 
63
            list($checks, $groups, $tags) = $this->getFilterParams($request);
Loading history...
63
64
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
65
66
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
67
            $runner->setBreakOnFailure($breakOnFailure);
68
69
            /** @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...
70
            $reporter = $this->reporterManager->getReporter('api');
71
72
            $runner->addReporter($reporter);
73
            $runner->run();
74
75
            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...
76
                'statusCode' => $reporter->getStatusCode(),
77
                'statusName' => $reporter->getStatusName(),
78
79
                'successes' => $reporter->getSuccessCount(),
80
                'warnings' => $reporter->getWarningCount(),
81
                'failures' => $reporter->getFailureCount(),
82
                'unknowns' => $reporter->getUnknownCount(),
83
                'total' => $reporter->getTotalCount(),
84
85
                'checks' => $reporter->getCheckResults(),
86
            ]);
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...
87
        } catch (\Exception $e) {
88
            $e = new HttpException(500, $e->getMessage());
89
90
            return new JsonResponse($e->toArray(), $e->getStatusCode());
91
        }
92
    }
93
94
    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...
95
    {
96
        try {
97
            list($checks, $groups, $tags) = $this->getFilterParams($request);
98
99
            $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...
100
                return null !== $i;
101
            });
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...
102
103
            if (null !== $checkSingle) {
104
                $runner = $this->runnerManager->getRunner($checkSingle);
105
            } else {
106
                $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
107
            }
108
109
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
110
            $runner->setBreakOnFailure($breakOnFailure);
111
112
            /** @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...
113
            $reporter = $this->reporterManager->getReporter('api');
114
115
            $runner->addReporter($reporter);
116
            $runner->run();
117
118
            if ($reporter->getTotalCount()) {
119
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500;
120
121
                return new Response($reporter->getStatusName(), $code);
122
            }
123
124
            throw new NotFoundHttpException('Check(s) not found');
125
        } catch (NotFoundHttpException $e) {
126
            return new Response($e->getMessage(), $e->getStatusCode());
127
        } catch (\Exception $e) {
128
            return new Response($e->getMessage(), 502);
129
        }
130
    }
131
}
132