Completed
Push — master ( f8d07f...897034 )
by Vladimir
05:58
created

TraitApiCheck   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkStatusAction() 0 29 5
A checkAction() 0 24 4
A checksAction() 0 34 3
A checkStatusesAction() 0 30 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\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): 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

32
    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...
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): Response
0 ignored issues
show
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
92 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
93
        }
94
    }
95
96 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...
97
    {
98
        try {
99 7
            $runner = $this->runnerManager->getRunner($id);
100
101 7
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
102 7
            $runner->setBreakOnFailure($breakOnFailure);
103
104
            /** @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...
105 7
            $reporter = $this->reporterManager->getReporter('api');
106
107 7
            $runner->addReporter($reporter);
108 7
            $runner->run();
109
110 7
            $res = $reporter->getCheckResults();
111
112 6
            if (isset($res[0])) {
113 5
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS
114 2
                    ? Response::HTTP_OK
115 5
                    : Response::HTTP_BAD_GATEWAY;
116
117 5
                return $this->creatResponse($reporter->getStatusName(), $code);
118
            }
119
120 1
            throw new NotFoundHttpException(sprintf('Check "%s" not found', $id));
121 2
        } catch (NotFoundHttpException $e) {
122 1
            return $this->creatResponse($e->getMessage(), $e->getStatusCode());
123 1
        } catch (\Exception $e) {
124 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
125
        }
126
    }
127
128 19
    public function checkStatusesAction(Request $request): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checkStatusesAction()
Loading history...
129
    {
130
        try {
131 19
            list($ids, $checks, $groups, $tags) = $this->getFilterParams($request);
132 19
            $checks = $checks ? $checks : $ids;
133
134 19
            $runner = $this->runnerManager->getRunner($checks, $groups, $tags);
135
136 19
            $breakOnFailure = (bool) $request->get('break-on-failure', false);
137 19
            $runner->setBreakOnFailure($breakOnFailure);
138
139
            /** @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...
140 19
            $reporter = $this->reporterManager->getReporter('api');
141 19
            $runner->addReporter($reporter);
142
143 19
            $runner->run();
144
145 19
            if ($reporter->getTotalCount()) {
146 14
                $code = $reporter->getStatusCode() === $reporter::STATUS_CODE_SUCCESS
147 4
                    ? Response::HTTP_OK
148 14
                    : Response::HTTP_BAD_GATEWAY;
149
150 14
                return $this->creatResponse($reporter->getStatusName(), $code);
151
            }
152
153 4
            throw new NotFoundHttpException('Check(s) not found');
154 5
        } catch (NotFoundHttpException $e) {
155 4
            return $this->creatResponse($e->getMessage(), $e->getStatusCode());
156 1
        } catch (\Exception $e) {
157 1
            return $this->creatResponse($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
158
        }
159
    }
160
}
161