Completed
Pull Request — master (#115)
by Sascha
02:17
created

HealthCheckController::listGroupsAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4286
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
namespace Liip\MonitorBundle\Controller;
4
5
use Liip\MonitorBundle\Helper\ArrayReporter;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Liip\MonitorBundle\Runner;
11
use Liip\MonitorBundle\Helper\PathHelper;
12
13
class HealthCheckController
14
{
15
    protected $container;
16
    protected $runner;
17
    protected $pathHelper;
18
    protected $template;
19
20
    /**
21
     * @param ContainerInterface $container
22
     * @param PathHelper         $pathHelper
23
     * @param                    $template
24
     */
25
    public function __construct(ContainerInterface $container, PathHelper $pathHelper, $template)
26
    {
27
        $this->container = $container;
28
        $this->pathHelper = $pathHelper;
29
        $this->template = $template;
30
    }
31
32
    /**
33
     * @param \Symfony\Component\HttpFoundation\Request $request
34
     *
35
     * @return \Symfony\Component\HttpFoundation\Response
36
     */
37
    public function indexAction(Request $request)
38
    {
39
        $group = $this->getGroup($request);
40
41
        $urls = $this->pathHelper->getRoutesJs(array(
42
            'liip_monitor_run_all_checks' => array('group' => $group),
43
            'liip_monitor_run_single_check' => array('checkId' => 'replaceme', 'group' => $group),
44
        ));
45
46
        $css = $this->pathHelper->getStyleTags(array(
47
            'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css',
48
            'bundles/liipmonitor/css/style.css',
49
        ));
50
51
        $javascript = $this->pathHelper->getScriptTags(array(
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, array('Content-Type' => 'text/html'));
64
    }
65
66
    /**
67
     * @return \Symfony\Component\HttpFoundation\Response
68
     */
69
    public function listAction(Request $request)
70
    {
71
        $ret = array();
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 = array();
88
        $runners = $this->container->getParameter('liip_monitor.runners');
89
90
        foreach ($runners as $runnerServiceId) {
91
            $runner = $this->container->get($runnerServiceId);
92
            $group = str_replace('liip_monitor.runner_', '', $runnerServiceId);
93
94
            foreach ($runner->getChecks() as $alias => $check) {
95
                $allChecks[$group][] = $alias;
96
            }
97
        }
98
99
        return new JsonResponse($allChecks);
100
    }
101
102
    /**
103
     * @return JsonResponse
104
     */
105
    public function listGroupsAction()
106
    {
107
        $groups = array();
108
        $runners = $this->container->getParameter('liip_monitor.runners');
109
110
        foreach ($runners as $runnerServiceId) {
111
            $group = str_replace('liip_monitor.runner_', '', $runnerServiceId);
112
            $groups[] = $group;
113
        }
114
115
        return new JsonResponse($groups);
116
    }
117
118
    /**
119
     * @param Request $request
120
     *
121
     * @return \Symfony\Component\HttpFoundation\Response
122
     */
123
    public function runAllChecksAction(Request $request)
124
    {
125
        $report = $this->runTests($request);
126
127
        return new JsonResponse(array(
128
            'checks' => $report->getResults(),
129
            'globalStatus' => $report->getGlobalStatus(),
130
        ));
131
    }
132
133
    /**
134
     * @param Request $request
135
     *
136
     * @return \Symfony\Component\HttpFoundation\Response
137
     */
138
    public function runAllChecksHttpStatusAction(Request $request)
139
    {
140
        $report = $this->runTests($request);
141
142
        return new Response(
143
            '',
144
            ($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
145
        );
146
    }
147
148
    /**
149
     * @param string  $checkId
150
     * @param Request $request
151
     *
152
     * @return \Symfony\Component\HttpFoundation\Response
153
     */
154
    public function runSingleCheckHttpStatusAction($checkId, Request $request)
155
    {
156
        $report = $this->runTests($request, $checkId);
157
158
        return new Response(
159
            '',
160
            ($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
161
        );
162
    }
163
164
    /**
165
     * @param string  $checkId
166
     * @param Request $request
167
     *
168
     * @return \Symfony\Component\HttpFoundation\Response
169
     */
170
    public function runSingleCheckAction($checkId, Request $request)
171
    {
172
        $results = $this->runTests($request, $checkId)->getResults();
173
174
        return new JsonResponse($results[0]);
175
    }
176
177
    /**
178
     * @param Request     $request
179
     * @param string|null $checkId
180
     *
181
     * @return ArrayReporter
182
     */
183
    protected function runTests(Request $request, $checkId = null)
184
    {
185
        $reporters = $request->query->get('reporters', array());
186
187
        if (!is_array($reporters)) {
188
            $reporters = array($reporters);
189
        }
190
191
        $reporter = new ArrayReporter();
192
193
        $runner = $this->getRunner($request);
194
195
        $runner->addReporter($reporter);
196
        $runner->useAdditionalReporters($reporters);
197
        $runner->run($checkId);
198
199
        return $reporter;
200
    }
201
202
    /**
203
     * @param Request $request
204
     *
205
     * @return Runner
206
     *
207
     * @throws \Exception
208
     */
209
    private function getRunner(Request $request)
210
    {
211
        $group = $this->getGroup($request);
212
213
        $runnerServiceId = 'liip_monitor.runner_'.$group;
214
215
        if ($this->container->has($runnerServiceId)) {
216
            return $this->container->get($runnerServiceId);
217
        }
218
219
        throw new \RuntimeException(sprintf('Unknown check group "%s"', $group));
220
    }
221
222
    /**
223
     * @param Request $request
224
     *
225
     * @return string
226
     */
227
    private function getGroup(Request $request)
228
    {
229
        $group = $request->query->get('group');
230
231
        if (!$group) {
232
            $group = $this->container->getParameter('liip_monitor.default_group');
233
        }
234
235
        return $group;
236
    }
237
}
238