Completed
Pull Request — master (#223)
by
unknown
15:19
created

HealthCheckController::getGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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