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

HealthCheckController::logTestStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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
23
     */
24
    protected $log;
25
26
    /**
27
     * @param RunnerManager        $runnerManager
28
     * @param PathHelper           $pathHelper
29
     * @param string               $template
30
     * @param LoggerInterface|null $log
31
     */
32
    public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, LoggerInterface $log = null)
33
    {
34
        $this->runnerManager = $runnerManager;
35
        $this->pathHelper = $pathHelper;
36
        $this->template = $template;
37
        if ($log === null) {
38
            $log = new NullLogger();
39
        }
40
        $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...
41
    }
42
43
    /**
44
     * @param \Symfony\Component\HttpFoundation\Request $request
45
     *
46
     * @return \Symfony\Component\HttpFoundation\Response
47
     */
48
    public function indexAction(Request $request)
49
    {
50
        $group = $this->getGroup($request);
51
52
        $urls = $this->pathHelper->getRoutesJs(array(
53
            'liip_monitor_run_all_checks' => array('group' => $group),
54
            'liip_monitor_run_single_check' => array('checkId' => 'replaceme', 'group' => $group),
55
        ));
56
57
        $css = $this->pathHelper->getStyleTags(array(
58
            'bundles/liipmonitor/css/bootstrap/css/bootstrap.min.css',
59
            'bundles/liipmonitor/css/style.css',
60
        ));
61
62
        $javascript = $this->pathHelper->getScriptTags(array(
63
            'bundles/liipmonitor/javascript/jquery-1.7.1.min.js',
64
            'bundles/liipmonitor/javascript/ember-0.9.5.min.js',
65
            'bundles/liipmonitor/javascript/app.js',
66
        ));
67
68
        // this is a hack to make the bundle template agnostic.
69
        // URL generation for Assets and Routes is still handled by the framework.
70
        ob_start();
71
        include $this->template;
72
        $content = ob_get_clean();
73
74
        $this->log->debug('HealthCheckController | index');
75
        return new Response($content, 200, array('Content-Type' => 'text/html'));
76
    }
77
78
    /**
79
     * @return \Symfony\Component\HttpFoundation\Response
80
     */
81
    public function listAction(Request $request)
82
    {
83
        $ret = array();
84
85
        $runner = $this->getRunner($request);
86
87
        foreach ($runner->getChecks() as $alias => $check) {
88
            $ret[] = $alias;
89
        }
90
91
        return new JsonResponse($ret);
92
    }
93
94
    /**
95
     * @return JsonResponse
96
     */
97
    public function listAllAction()
98
    {
99
        $allChecks = array();
100
101
        foreach ($this->runnerManager->getRunners() as $group => $runner) {
102
            foreach ($runner->getChecks() as $alias => $check) {
103
                $allChecks[$group][] = $alias;
104
            }
105
        }
106
107
        return new JsonResponse($allChecks);
108
    }
109
110
    /**
111
     * @return JsonResponse
112
     */
113
    public function listGroupsAction()
114
    {
115
        $groups = $this->runnerManager->getGroups();
116
117
        return new JsonResponse($groups);
118
    }
119
120
    /**
121
     * @param Request $request
122
     *
123
     * @return \Symfony\Component\HttpFoundation\Response
124
     */
125
    public function runAllChecksAction(Request $request)
126
    {
127
        $report = $this->runTests($request);
128
129
        return new JsonResponse(array(
130
            'checks' => $report->getResults(),
131
            'globalStatus' => $report->getGlobalStatus(),
132
        ));
133
    }
134
135
    /**
136
     * @param Request $request
137
     *
138
     * @return \Symfony\Component\HttpFoundation\Response
139
     */
140
    public function runAllChecksHttpStatusAction(Request $request)
141
    {
142
        $report = $this->runTests($request);
143
144
        return new Response(
145
            '',
146
            ($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
147
        );
148
    }
149
150
    /**
151
     * @param string  $checkId
152
     * @param Request $request
153
     *
154
     * @return \Symfony\Component\HttpFoundation\Response
155
     */
156
    public function runSingleCheckHttpStatusAction($checkId, Request $request)
157
    {
158
        $report = $this->runTests($request, $checkId);
159
160
        return new Response(
161
            '',
162
            ($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
163
        );
164
    }
165
166
    /**
167
     * @param string  $checkId
168
     * @param Request $request
169
     *
170
     * @return \Symfony\Component\HttpFoundation\Response
171
     */
172
    public function runSingleCheckAction($checkId, Request $request)
173
    {
174
        $results = $this->runTests($request, $checkId)->getResults();
175
176
        return new JsonResponse($results[0]);
177
    }
178
179
    /**
180
     * @param Request     $request
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', array());
188
189
        if (!is_array($reporters)) {
190
            $reporters = array($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
        $this->logTestStatus($reporter->getGlobalStatus() === ArrayReporter::STATUS_OK, $reporter);
202
203
        return $reporter;
204
    }
205
206
    private function statusIsNotOK(array $resultItem)
207
    {
208
        return $resultItem['status'] !== 0;
209
    }
210
211
    /**
212
     * Send results to the Log.
213
     *
214
     * @param bool          $isOK     Did all the checks all returned OK?
215
     * @param ArrayReporter $reporter Details of the tests
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