Completed
Push — master ( 8cfc77...3ca914 )
by Vladimir
06:29
created

ApiCheckTrait::checkStatusesAction()   A

Complexity

Conditions 5
Paths 27

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0488

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
ccs 14
cts 16
cp 0.875
rs 9.4222
c 0
b 0
f 0
cc 5
nc 27
nop 1
crap 5.0488
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 JMS\Serializer\Serializer;
19
use Tvi\MonitorBundle\Exception\HttpException;
20
use Tvi\MonitorBundle\Reporter\Api;
21
use Tvi\MonitorBundle\Reporter\ReporterManager;
22
use Tvi\MonitorBundle\Runner\RunnerManager;
23
24
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
 * @property RunnerManager   $runnerManager
26
 * @property ReporterManager $reporterManager
27
 * @property Serializer      $serializer
28
 *
29
 * @author Vladimir Turnaev <[email protected]>
30
 */
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...
31
trait ApiCheckTrait
32
{
33 18
    public function checkAction(Request $request, string $id): 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

33
    public function checkAction(/** @scrutinizer ignore-unused */ Request $request, string $id): 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...
34
    {
35
        try {
36 18
            $runner = $this->runnerManager->getRunner($id);
37
38
            /** @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...
39 18
            $reporter = $this->reporterManager->getReporter('api');
40
41 18
            $runner->addReporter($reporter);
42 18
            $runner->run();
43
44 18
            $res = $reporter->getCheckResults();
45
46 18
            if (isset($res[0])) {
47 17
                return JsonResponse::fromJsonString($this->serializer->serialize($res[0], 'json'));
48
            }
49
50 1
            throw new NotFoundHttpException(sprintf('Check %s not found', $id));
51 1
        } catch (NotFoundHttpException $e) {
52 1
            $e = new HttpException(404, $e->getMessage());
53 1
            $json = $this->serializer->serialize($e->toArray(), 'json');
54
55 1
            return JsonResponse::fromJsonString($json, $e->getStatusCode());
56
        } catch (\Exception $e) {
57
            $e = new HttpException(500, $e->getMessage());
58
59
            $data = $this->serializer->serialize($e->toArray(), 'json');
60
61
            return JsonResponse::fromJsonString($data, $e->getStatusCode());
62
        }
63
    }
64
65 12
    public function checksAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checksAction()
Loading history...
66
    {
67
        try {
68 12
            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

68
            /** @scrutinizer ignore-call */ 
69
            list($checks, $groups, $tags) = $this->getFilterParams($request);
Loading history...
69
70 12
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
71
72 12
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
73 12
            $runner->setBreakOnFailure($breakOnFailure);
74
75
            /** @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...
76 12
            $reporter = $this->reporterManager->getReporter('api');
77
78 12
            $runner->addReporter($reporter);
79 12
            $runner->run();
80
            $data = [
81 12
                'statusCode' => $reporter->getStatusCode(),
82 12
                'statusName' => $reporter->getStatusName(),
83
84 12
                'successes' => $reporter->getSuccessCount(),
85 12
                'warnings' => $reporter->getWarningCount(),
86 12
                'failures' => $reporter->getFailureCount(),
87 12
                'unknowns' => $reporter->getUnknownCount(),
88 12
                'total' => $reporter->getTotalCount(),
89
90 12
                'checks' => $reporter->getCheckResults(),
91
            ];
92 12
            $json = $this->serializer->serialize($data, 'json');
93
94 12
            return JsonResponse::fromJsonString($json);
95
        } catch (\Exception $e) {
96
            $e = new HttpException(500, $e->getMessage());
97
            $json = $this->serializer->serialize($e->toArray(), 'json');
98
99
            return JsonResponse::fromJsonString($json, $e->getStatusCode());
100
        }
101
    }
102
103 5
    public function checkStatusAction(Request $request, string $id): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkStatusAction()
Loading history...
104
    {
105
        try {
106 5
            $runner = $this->runnerManager->getRunner($id);
107
108 5
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
109 5
            $runner->setBreakOnFailure($breakOnFailure);
110
111
            /** @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...
112 5
            $reporter = $this->reporterManager->getReporter('api');
113
114 5
            $runner->addReporter($reporter);
115 5
            $runner->run();
116
117 5
            if ($reporter->getTotalCount()) {
118 5
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500;
119
120 5
                return new Response($reporter->getStatusName(), $code);
121
            }
122
123
            throw new NotFoundHttpException(sprintf('Check %s not found', $id));
124
        } catch (NotFoundHttpException $e) {
125
            return new Response($e->getMessage(), $e->getStatusCode());
126
        } catch (\Exception $e) {
127
            return new Response($e->getMessage(), 502);
128
        }
129
    }
130
131 16
    public function checkStatusesAction(Request $request): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkStatusesAction()
Loading history...
132
    {
133
        try {
134 16
            list($checks, $groups, $tags) = $this->getFilterParams($request);
135
136 16
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
137
138 16
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
139 16
            $runner->setBreakOnFailure($breakOnFailure);
140
141
            /** @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...
142 16
            $reporter = $this->reporterManager->getReporter('api');
143
144 16
            $runner->addReporter($reporter);
145 16
            $runner->run();
146
147 16
            if ($reporter->getTotalCount()) {
148 13
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS ? 200 : 500;
149
150 13
                return new Response($reporter->getStatusName(), $code);
151
            }
152
153 3
            throw new NotFoundHttpException('Check(s) not found');
154 3
        } catch (NotFoundHttpException $e) {
155 3
            return new Response($e->getMessage(), $e->getStatusCode());
156
        } catch (\Exception $e) {
157
            return new Response($e->getMessage(), 502);
158
        }
159
    }
160
}
161