HealthCheckController   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 204
Duplicated Lines 8.82 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 8
dl 18
loc 204
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A listAction() 0 12 2
A listAllAction() 0 12 3
A listGroupsAction() 0 6 1
A runSingleCheckHttpStatusAction() 9 9 2
A listReportersAction() 0 4 1
A __construct() 0 7 1
A indexAction() 0 28 1
A runAllChecksAction() 0 9 1
A runAllChecksHttpStatusAction() 9 9 2
A runSingleCheckAction() 0 6 1
A runTests() 0 18 2
A getRunner() 0 12 2
A getGroup() 0 4 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Liip\MonitorBundle\Controller;
4
5
use Liip\MonitorBundle\Helper\ArrayReporter;
6
use Liip\MonitorBundle\Helper\PathHelper;
7
use Liip\MonitorBundle\Helper\RunnerManager;
8
use Liip\MonitorBundle\Runner;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class HealthCheckController
14
{
15
    protected $runnerManager;
16
    protected $pathHelper;
17
    protected $template;
18
    protected $failureStatusCode;
19
20
    /**
21
     * @param $template
22
     * @param $failureStatusCode
23
     */
24
    public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, $failureStatusCode)
25
    {
26
        $this->runnerManager = $runnerManager;
27
        $this->pathHelper = $pathHelper;
28
        $this->template = $template;
29
        $this->failureStatusCode = $failureStatusCode;
30
    }
31
32
    /**
33
     * @return Response
34
     */
35
    public function indexAction(Request $request)
36
    {
37
        $group = $this->getGroup($request);
38
39
        $urls = $this->pathHelper->getRoutesJs([
40
            'liip_monitor_run_all_checks' => ['group' => $group],
41
            'liip_monitor_run_single_check' => ['checkId' => 'replaceme', 'group' => $group],
42
        ]);
43
44
        $css = $this->pathHelper->getStyleTags([
45
            'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css',
46
            'bundles/liipmonitor/css/style.css',
47
        ]);
48
49
        $javascript = $this->pathHelper->getScriptTags([
50
            'bundles/liipmonitor/javascript/jquery-1.7.1.min.js',
51
            'bundles/liipmonitor/javascript/ember-0.9.5.min.js',
52
            'bundles/liipmonitor/javascript/app.js',
53
        ]);
54
55
        // this is a hack to make the bundle template agnostic.
56
        // URL generation for Assets and Routes is still handled by the framework.
57
        ob_start();
58
        include $this->template;
59
        $content = ob_get_clean();
60
61
        return new Response($content, 200, ['Content-Type' => 'text/html']);
62
    }
63
64
    /**
65
     * @return Response
66
     */
67
    public function listAction(Request $request)
68
    {
69
        $ret = [];
70
71
        $runner = $this->getRunner($request);
72
73
        foreach ($runner->getChecks() as $alias => $check) {
74
            $ret[] = $alias;
75
        }
76
77
        return new JsonResponse($ret);
78
    }
79
80
    /**
81
     * @return JsonResponse
82
     */
83
    public function listAllAction()
84
    {
85
        $allChecks = [];
86
87
        foreach ($this->runnerManager->getRunners() as $group => $runner) {
88
            foreach ($runner->getChecks() as $alias => $check) {
89
                $allChecks[$group][] = $alias;
90
            }
91
        }
92
93
        return new JsonResponse($allChecks);
94
    }
95
96
    /**
97
     * @return JsonResponse
98
     */
99
    public function listGroupsAction()
100
    {
101
        $groups = $this->runnerManager->getGroups();
102
103
        return new JsonResponse($groups);
104
    }
105
106
    /**
107
     * @return Response
108
     */
109
    public function runAllChecksAction(Request $request)
110
    {
111
        $report = $this->runTests($request);
112
113
        return new JsonResponse([
114
            'checks' => $report->getResults(),
115
            'globalStatus' => $report->getGlobalStatus(),
116
        ]);
117
    }
118
119
    /**
120
     * @return Response
121
     */
122 View Code Duplication
    public function runAllChecksHttpStatusAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $report = $this->runTests($request);
125
126
        return new Response(
127
            '',
128
            (ArrayReporter::STATUS_OK === $report->getGlobalStatus() ? 200 : $this->failureStatusCode)
129
        );
130
    }
131
132
    /**
133
     * @param string $checkId
134
     *
135
     * @return Response
136
     */
137 View Code Duplication
    public function runSingleCheckHttpStatusAction($checkId, Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        $report = $this->runTests($request, $checkId);
140
141
        return new Response(
142
            '',
143
            (ArrayReporter::STATUS_OK === $report->getGlobalStatus() ? 200 : $this->failureStatusCode)
144
        );
145
    }
146
147
    /**
148
     * @param string $checkId
149
     *
150
     * @return Response
151
     */
152
    public function runSingleCheckAction($checkId, Request $request)
153
    {
154
        $results = $this->runTests($request, $checkId)->getResults();
155
156
        return new JsonResponse($results[0]);
157
    }
158
159
    /**
160
     * @param string|null $checkId
161
     *
162
     * @return ArrayReporter
163
     */
164
    protected function runTests(Request $request, $checkId = null)
165
    {
166
        $reporters = $request->query->get('reporters') ?? [];
167
168
        if (!is_array($reporters)) {
169
            $reporters = [$reporters];
170
        }
171
172
        $reporter = new ArrayReporter();
173
174
        $runner = $this->getRunner($request);
175
176
        $runner->addReporter($reporter);
177
        $runner->useAdditionalReporters($reporters);
178
        $runner->run($checkId);
179
180
        return $reporter;
181
    }
182
183
    /**
184
     * @return Runner
185
     *
186
     * @throws \Exception
187
     */
188
    private function getRunner(Request $request)
189
    {
190
        $group = $this->getGroup($request);
191
192
        $runner = $this->runnerManager->getRunner($group);
193
194
        if ($runner) {
195
            return $runner;
196
        }
197
198
        throw new \RuntimeException(sprintf('Unknown check group "%s"', $group));
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    private function getGroup(Request $request)
205
    {
206
        return $request->query->get('group') ?: $this->runnerManager->getDefaultGroup();
207
    }
208
209
    /**
210
     * @return Response
211
     */
212
    public function listReportersAction()
213
    {
214
        return new JsonResponse($this->runnerManager->getReporters());
215
    }
216
}
217