TraitApiCheck::checkStatusesAction()   A
last analyzed

Complexity

Conditions 6
Paths 51

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 51
nop 2
dl 0
loc 30
ccs 19
cts 19
cp 1
crap 6
rs 9.0111
c 0
b 0
f 0
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\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
17
use JMS\Serializer\Serializer;
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
 * @property Serializer      $serializer
27
 *
28
 * @author Vladimir Turnaev <[email protected]>
29
 */
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...
30
trait TraitApiCheck
31
{
32 19
    public function checkAction(Request $request, string $id, string $version): Response
0 ignored issues
show
Unused Code introduced by
The parameter $version 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

32
    public function checkAction(Request $request, string $id, /** @scrutinizer ignore-unused */ string $version): Response

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...
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

32
    public function checkAction(/** @scrutinizer ignore-unused */ Request $request, string $id, string $version): Response

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...
33
    {
34
        try {
35 19
            $runner = $this->runnerManager->getRunner($id);
36
37
            /** @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...
38 19
            $reporter = $this->reporterManager->getReporter('api');
39 19
            $runner->addReporter($reporter);
40
41 19
            $runner->run();
42
43 19
            $res = $reporter->getCheckResults();
44
45 18
            if (isset($res[0])) {
46 17
                return $this->creatResponse($res[0], Response::HTTP_OK, true);
0 ignored issues
show
Bug introduced by
It seems like creatResponse() 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

46
                return $this->/** @scrutinizer ignore-call */ creatResponse($res[0], Response::HTTP_OK, true);
Loading history...
47
            }
48
49 1
            throw new NotFoundHttpException(sprintf('Check %s not found', $id));
50 2
        } catch (NotFoundHttpException $e) {
51 1
            $e = new HttpException($e->getStatusCode(), $e->getMessage());
52
53 1
            return $this->creatResponse($e->toArray(), $e->getStatusCode(), true);
54 1
        } catch (\Exception $e) {
55 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
56
        }
57
    }
58
59 15
    public function checksAction(Request $request, string $version): Response
0 ignored issues
show
Unused Code introduced by
The parameter $version 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

59
    public function checksAction(Request $request, /** @scrutinizer ignore-unused */ string $version): Response

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 checksAction()
Loading history...
60
    {
61
        try {
62 15
            list($ids, $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($ids, $checks, $groups, $tags) = $this->getFilterParams($request);
Loading history...
63 15
            $checks = $checks ? $checks : $ids;
64
65 15
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
66
67 15
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
68 15
            $runner->setBreakOnFailure($breakOnFailure);
69
70
            /** @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...
71 15
            $reporter = $this->reporterManager->getReporter('api');
72
73 15
            $runner->addReporter($reporter);
74 15
            $runner->run();
75
76
            $data = [
77 15
                'statusCode' => $reporter->getStatusCode(),
78 14
                'statusName' => $reporter->getStatusName(),
79
80 14
                'successes' => $reporter->getSuccessCount(),
81 14
                'warnings' => $reporter->getWarningCount(),
82 14
                'failures' => $reporter->getFailureCount(),
83 14
                'unknowns' => $reporter->getUnknownCount(),
84 14
                'total' => $reporter->getTotalCount(),
85
86 14
                'checks' => $reporter->getCheckResults(),
87
            ];
88
89 14
            return $this->creatResponse($data, Response::HTTP_OK, true);
90 1
        } catch (\Exception $e) {
91 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
92
        }
93
    }
94
95 7
    public function checkStatusAction(Request $request, string $id, string $version): Response
0 ignored issues
show
Unused Code introduced by
The parameter $version 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

95
    public function checkStatusAction(Request $request, string $id, /** @scrutinizer ignore-unused */ string $version): Response

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 checkStatusAction()
Loading history...
96
    {
97
        try {
98 7
            $runner = $this->runnerManager->getRunner($id);
99
100 7
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
101 7
            $runner->setBreakOnFailure($breakOnFailure);
102
103
            /** @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...
104 7
            $reporter = $this->reporterManager->getReporter('api');
105
106 7
            $runner->addReporter($reporter);
107 7
            $runner->run();
108
109 7
            $res = $reporter->getCheckResults();
110
111 6
            if (isset($res[0])) {
112 5
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS
113 2
                    ? Response::HTTP_OK
114 5
                    : Response::HTTP_BAD_GATEWAY;
115
116 5
                return $this->creatResponse($reporter->getStatusName(), $code);
117
            }
118
119 1
            throw new NotFoundHttpException(sprintf('Check "%s" not found', $id));
120 2
        } catch (NotFoundHttpException $e) {
121 1
            return $this->creatResponse($e->getMessage(), $e->getStatusCode());
122 1
        } catch (\Exception $e) {
123 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
124
        }
125
    }
126
127 19
    public function checkStatusesAction(Request $request, string $version): Response
0 ignored issues
show
Unused Code introduced by
The parameter $version 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

127
    public function checkStatusesAction(Request $request, /** @scrutinizer ignore-unused */ string $version): Response

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 checkStatusesAction()
Loading history...
128
    {
129
        try {
130 19
            list($ids, $checks, $groups, $tags) = $this->getFilterParams($request);
131 19
            $checks = $checks ? $checks : $ids;
132
133 19
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
134
135 19
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
136 19
            $runner->setBreakOnFailure($breakOnFailure);
137
138
            /** @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...
139 19
            $reporter = $this->reporterManager->getReporter('api');
140 19
            $runner->addReporter($reporter);
141
142 19
            $runner->run();
143
144 19
            if ($reporter->getTotalCount()) {
145 14
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS
146 4
                    ? Response::HTTP_OK
147 14
                    : Response::HTTP_BAD_GATEWAY;
148
149 14
                return $this->creatResponse($reporter->getStatusName(), $code);
150
            }
151
152 4
            throw new NotFoundHttpException('Check(s) not found');
153 5
        } catch (NotFoundHttpException $e) {
154 4
            return $this->creatResponse($e->getMessage(), $e->getStatusCode());
155 1
        } catch (\Exception $e) {
156 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
157
        }
158
    }
159
}
160