Completed
Push — master ( 033231...4a0dad )
by Kevin
12s queued 10s
created

HealthCheckController::runTests()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 13
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
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\Helper\StreamedReporter;
9
use Liip\MonitorBundle\Runner;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\StreamedResponse;
14
15
class HealthCheckController
16
{
17
    protected $runnerManager;
18
    protected $pathHelper;
19
    protected $template;
20
    protected $failureStatusCode;
21
    protected $isLazyRun;
22
23
    /**
24
     * @param $template
25
     * @param $failureStatusCode
26
     * @param $isLazyRun
27
     */
28
    public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, $failureStatusCode, $isLazyRun)
29
    {
30
        $this->runnerManager = $runnerManager;
31
        $this->pathHelper = $pathHelper;
32
        $this->template = $template;
33
        $this->failureStatusCode = $failureStatusCode;
34
        $this->isLazyRun = $isLazyRun;
35
    }
36
37
    /**
38
     * @return Response
39
     */
40
    public function indexAction(Request $request)
41
    {
42
        $group = $this->getGroup($request);
43
44
        $urls = $this->pathHelper->getRoutesJs([
45
            'liip_monitor_run_all_checks' => ['group' => $group],
46
            'liip_monitor_stream_all_checks' => ['group' => $group],
47
            'liip_monitor_run_single_check' => ['checkId' => 'replaceme', 'group' => $group],
48
        ]);
49
50
        $css = $this->pathHelper->getStyleTags([
51
            'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css',
52
            'bundles/liipmonitor/css/style.css',
53
        ]);
54
55
        $javascript = $this->pathHelper->getScriptTags([
56
            'bundles/liipmonitor/javascript/jquery-1.7.1.min.js',
57
            'bundles/liipmonitor/javascript/ember-0.9.5.min.js',
58
            'bundles/liipmonitor/javascript/app.js',
59
        ]);
60
61
        $isLazyRun = $this->isLazyRun ? 1 : 0;
62
63
        // this is a hack to make the bundle template agnostic.
64
        // URL generation for Assets and Routes is still handled by the framework.
65
        ob_start();
66
        include $this->template;
67
        $content = ob_get_clean();
68
69
        return new Response($content, 200, ['Content-Type' => 'text/html']);
70
    }
71
72
    /**
73
     * @return Response
74
     */
75
    public function listAction(Request $request)
76
    {
77
        $ret = [];
78
79
        $runner = $this->getRunner($request);
80
81
        foreach ($runner->getChecks() as $alias => $check) {
82
            $ret[] = $alias;
83
        }
84
85
        return new JsonResponse($ret);
86
    }
87
88
    /**
89
     * @return JsonResponse
90
     */
91
    public function listAllAction()
92
    {
93
        $allChecks = [];
94
95
        foreach ($this->runnerManager->getRunners() as $group => $runner) {
96
            foreach ($runner->getChecks() as $alias => $check) {
97
                $allChecks[$group][] = $alias;
98
            }
99
        }
100
101
        return new JsonResponse($allChecks);
102
    }
103
104
    /**
105
     * @return JsonResponse
106
     */
107
    public function listGroupsAction()
108
    {
109
        $groups = $this->runnerManager->getGroups();
110
111
        return new JsonResponse($groups);
112
    }
113
114
    /**
115
     * @return Response
116
     */
117
    public function runAllChecksAction(Request $request)
118
    {
119
        $report = $this->runTests($request);
120
121
        return new JsonResponse([
122
            'checks' => $report->getResults(),
123
            'globalStatus' => $report->getGlobalStatus(),
124
        ]);
125
    }
126
127
    public function streamAllChecksAction(Request $request)
128
    {
129
        return new StreamedResponse(
130
            function () use ($request) {
131
                $reporter = new StreamedReporter();
132
                $runner = $this->getRunner($request);
133
134
                $runner->addReporter($reporter);
135
                $runner->run();
136
            }
137
        );
138
    }
139
140
    /**
141
     * @return Response
142
     */
143 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...
144
    {
145
        $report = $this->runTests($request);
146
147
        return new Response(
148
            '',
149
            (ArrayReporter::STATUS_OK === $report->getGlobalStatus() ? 200 : $this->failureStatusCode)
150
        );
151
    }
152
153
    /**
154
     * @param string $checkId
155
     *
156
     * @return Response
157
     */
158 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...
159
    {
160
        $report = $this->runTests($request, $checkId);
161
162
        return new Response(
163
            '',
164
            (ArrayReporter::STATUS_OK === $report->getGlobalStatus() ? 200 : $this->failureStatusCode)
165
        );
166
    }
167
168
    /**
169
     * @param string $checkId
170
     *
171
     * @return Response
172
     */
173
    public function runSingleCheckAction($checkId, Request $request)
174
    {
175
        $results = $this->runTests($request, $checkId)->getResults();
176
177
        return new JsonResponse($results[0]);
178
    }
179
180
    /**
181
     * @param string|null $checkId
182
     *
183
     * @return ArrayReporter
184
     */
185
    protected function runTests(Request $request, $checkId = null)
186
    {
187
        $reporters = $request->query->get('reporters', []);
188
189
        if (!is_array($reporters)) {
190
            $reporters = [$reporters];
191
        }
192
193
        $reporter = new ArrayReporter();
194
195
        $runner = $this->getRunner($request);
196
197
        $runner->addReporter($reporter);
198
        $runner->useAdditionalReporters($reporters);
199
        $runner->run($checkId);
200
201
        return $reporter;
202
    }
203
204
    /**
205
     * @return Runner
206
     *
207
     * @throws \Exception
208
     */
209
    private function getRunner(Request $request)
210
    {
211
        $group = $this->getGroup($request);
212
213
        $runner = $this->runnerManager->getRunner($group);
214
215
        if ($runner) {
216
            return $runner;
217
        }
218
219
        throw new \RuntimeException(sprintf('Unknown check group "%s"', $group));
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    private function getGroup(Request $request)
226
    {
227
        return $request->query->get('group') ?: $this->runnerManager->getDefaultGroup();
228
    }
229
230
    /**
231
     * @return Response
232
     */
233
    public function listReportersAction()
234
    {
235
        return new JsonResponse($this->runnerManager->getReporters());
236
    }
237
}
238