Completed
Push — master ( 3ca914...0fe9b6 )
by Vladimir
07:36 queued 01:23
created

ApiCheckTrait::checksAction()   A

Complexity

Conditions 2
Paths 11

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2.004

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
ccs 18
cts 20
cp 0.9
rs 9.584
c 0
b 0
f 0
cc 2
nc 11
nop 1
crap 2.004
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
            return new Response($e->getMessage(), 500);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Symfony\Compo...($e->getMessage(), 500) returns the type Symfony\Component\HttpFoundation\Response which includes types incompatible with the type-hinted return Symfony\Component\HttpFoundation\JsonResponse.
Loading history...
58
        }
59
    }
60
61 12
    public function checksAction(Request $request): JsonResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function checksAction()
Loading history...
62
    {
63
        try {
64 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

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