Completed
Push — master ( 90448b...2ebf44 )
by Vladimir
05:45 queued 30s
created

ApiCheckTrait   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
eloc 75
dl 0
loc 129
ccs 73
cts 73
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkAction() 0 25 4
A checkStatusAction() 0 29 5
A checkStatusesAction() 0 30 6
A checksAction() 0 34 3
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 19
    public function checkAction(Request $request, string $id): Response
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): 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...
34
    {
35
        try {
36 19
            $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 19
            $reporter = $this->reporterManager->getReporter('api');
40 19
            $runner->addReporter($reporter);
41
42 19
            $runner->run();
43
44 19
            $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 2
        } catch (NotFoundHttpException $e) {
52 1
            $e = new HttpException($e->getStatusCode(), $e->getMessage());
53 1
            $json = $this->serializer->serialize($e->toArray(), 'json');
54
55 1
            return JsonResponse::fromJsonString($json, $e->getStatusCode());
56 1
        } catch (\Exception $e) {
57 1
            return new Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
58
        }
59
    }
60
61 15
    public function checksAction(Request $request): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checksAction()
Loading history...
62
    {
63
        try {
64 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

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