Completed
Pull Request — master (#130)
by Alister
02:10
created

HealthCheckController::logTestStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace Liip\MonitorBundle\Controller;
4
5
use Liip\MonitorBundle\Helper\ArrayReporter;
6
use Liip\MonitorBundle\Helper\RunnerManager;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Liip\MonitorBundle\Runner;
13
use Liip\MonitorBundle\Helper\PathHelper;
14
15
class HealthCheckController
16
{
17
    protected $runnerManager;
18
    protected $pathHelper;
19
    protected $template;
20
21
    /**
22
     * @var Psr\Log\LoggerInterface $log
23
     */
24
    protected $log;
25
26
    /**
27
     * @param RunnerManager $runnerManager
28
     * @param PathHelper    $pathHelper
29
     * @param               $template
30
     */
31
    public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, LoggerInterface $log = null)
32
    {
33
        $this->runnerManager = $runnerManager;
34
        $this->pathHelper = $pathHelper;
35
        $this->template = $template;
36
        if ($log === null) {
37
            $log = new NullLogger();
38
        }
39
        $this->log = $log;
0 ignored issues
show
Documentation Bug introduced by
It seems like $log of type object<Psr\Log\LoggerInterface> is incompatible with the declared type object<Liip\MonitorBundl...sr\Log\LoggerInterface> of property $log.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
    }
41
42
    /**
43
     * @param \Symfony\Component\HttpFoundation\Request $request
44
     *
45
     * @return \Symfony\Component\HttpFoundation\Response
46
     */
47
    public function indexAction(Request $request)
48
    {
49
        $group = $this->getGroup($request);
50
51
        $urls = $this->pathHelper->getRoutesJs(array(
52
            'liip_monitor_run_all_checks' => array('group' => $group),
53
            'liip_monitor_run_single_check' => array('checkId' => 'replaceme', 'group' => $group),
54
        ));
55
56
        $css = $this->pathHelper->getStyleTags(array(
57
            'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css',
58
            'bundles/liipmonitor/css/style.css',
59
        ));
60
61
        $javascript = $this->pathHelper->getScriptTags(array(
62
            'bundles/liipmonitor/javascript/jquery-1.7.1.min.js',
63
            'bundles/liipmonitor/javascript/ember-0.9.5.min.js',
64
            'bundles/liipmonitor/javascript/app.js',
65
        ));
66
67
        // this is a hack to make the bundle template agnostic.
68
        // URL generation for Assets and Routes is still handled by the framework.
69
        ob_start();
70
        include $this->template;
71
        $content = ob_get_clean();
72
73
        $this->log->debug('HealthCheckController | index');
74
        return new Response($content, 200, array('Content-Type' => 'text/html'));
75
    }
76
77
    /**
78
     * @return \Symfony\Component\HttpFoundation\Response
79
     */
80
    public function listAction(Request $request)
81
    {
82
        $ret = array();
83
84
        $runner = $this->getRunner($request);
85
86
        foreach ($runner->getChecks() as $alias => $check) {
87
            $ret[] = $alias;
88
        }
89
90
        return new JsonResponse($ret);
91
    }
92
93
    /**
94
     * @return JsonResponse
95
     */
96
    public function listAllAction()
97
    {
98
        $allChecks = array();
99
100
        foreach ($this->runnerManager->getRunners() as $group => $runner) {
101
            foreach ($runner->getChecks() as $alias => $check) {
102
                $allChecks[$group][] = $alias;
103
            }
104
        }
105
106
        return new JsonResponse($allChecks);
107
    }
108
109
    /**
110
     * @return JsonResponse
111
     */
112
    public function listGroupsAction()
113
    {
114
        $groups = $this->runnerManager->getGroups();
115
116
        return new JsonResponse($groups);
117
    }
118
119
    /**
120
     * @param Request $request
121
     *
122
     * @return \Symfony\Component\HttpFoundation\Response
123
     */
124
    public function runAllChecksAction(Request $request)
125
    {
126
        $report = $this->runTests($request);
127
128
        return new JsonResponse(array(
129
            'checks' => $report->getResults(),
130
            'globalStatus' => $report->getGlobalStatus(),
131
        ));
132
    }
133
134
    /**
135
     * @param Request $request
136
     *
137
     * @return \Symfony\Component\HttpFoundation\Response
138
     */
139
    public function runAllChecksHttpStatusAction(Request $request)
140
    {
141
        $report = $this->runTests($request);
142
143
        return new Response(
144
            '',
145
            ($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
146
        );
147
    }
148
149
    /**
150
     * @param string  $checkId
151
     * @param Request $request
152
     *
153
     * @return \Symfony\Component\HttpFoundation\Response
154
     */
155
    public function runSingleCheckHttpStatusAction($checkId, Request $request)
156
    {
157
        $report = $this->runTests($request, $checkId);
158
159
        return new Response(
160
            '',
161
            ($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
162
        );
163
    }
164
165
    /**
166
     * @param string  $checkId
167
     * @param Request $request
168
     *
169
     * @return \Symfony\Component\HttpFoundation\Response
170
     */
171
    public function runSingleCheckAction($checkId, Request $request)
172
    {
173
        $results = $this->runTests($request, $checkId)->getResults();
174
175
        return new JsonResponse($results[0]);
176
    }
177
178
    /**
179
     * @param Request     $request
180
     * @param string|null $checkId
181
     *
182
     * @return ArrayReporter
183
     */
184
    protected function runTests(Request $request, $checkId = null)
185
    {
186
        $reporters = $request->query->get('reporters', array());
187
188
        if (!is_array($reporters)) {
189
            $reporters = array($reporters);
190
        }
191
192
        $reporter = new ArrayReporter();
193
194
        $runner = $this->getRunner($request);
195
196
        $runner->addReporter($reporter);
197
        $runner->useAdditionalReporters($reporters);
198
        $runner->run($checkId);
199
200
        $this->logTestStatus($reporter->getGlobalStatus() === ArrayReporter::STATUS_OK, $reporter);
201
        return $reporter;
202
    }
203
204
    private function statusIsNotOK(array $resultItem)
205
    {
206
        return $resultItem['status'] !== 0;
207
    }
208
209
    /**
210
     * Send results to the Log.
211
     *
212
     * @param boolean       $isOK     Did all the checks all returned OK?
213
     * @param ArrayReporter $reporter Details of the tests
214
     *
215
     * @return void
216
     */
217
    private function logTestStatus($isOK, ArrayReporter $reporter)
218
    {
219
        if ($isOK) {
220
            $this->log->debug('HealthCheckController | runTests, OK');
221
            return;
222
        }
223
224
        $failures = array_filter($reporter->getResults(), array($this, 'statusIsNotOK'));
225
        $jsonStr = json_encode($failures);
226
        $this->log->alert('HealthCheckController | runTests, FAILURE | ' . $jsonStr);
227
    }
228
229
    /**
230
     * @param Request $request
231
     *
232
     * @return Runner
233
     *
234
     * @throws \Exception
235
     */
236
    private function getRunner(Request $request)
237
    {
238
        $group = $this->getGroup($request);
239
240
        $runner = $this->runnerManager->getRunner($group);
241
242
        if ($runner) {
243
            return $runner;
244
        }
245
246
        throw new \RuntimeException(sprintf('Unknown check group "%s"', $group));
247
    }
248
249
    /**
250
     * @param Request $request
251
     *
252
     * @return string
253
     */
254
    private function getGroup(Request $request)
255
    {
256
        return $request->query->get('group') ?: $this->runnerManager->getDefaultGroup();
257
    }
258
}
259