Completed
Push — master ( 3f77a7...938ad9 )
by Andrii
03:48
created

DebugAction::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 9.0856
cc 2
eloc 14
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * Tools to use API as ActiveRecord for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\debug;
12
13
use Yii;
14
use yii\helpers\ArrayHelper;
15
use yii\web\HttpException;
16
use yii\web\Response;
17
18
/**
19
 * Debug Action is used by [[DebugPanel]] to perform HiArt queries using ajax.
20
 */
21
class DebugAction extends \yii\base\Action
22
{
23
    /**
24
     * @var DebugPanel
25
     */
26
    public $panel;
27
28
    /**
29
     * @var \yii\debug\controllers\DefaultController
30
     */
31
    public $controller;
32
33
    public function run($logId, $tag)
34
    {
35
        $this->controller->loadData($tag);
36
37
        $timings = $this->panel->getTimings();
38
        ArrayHelper::multisort($timings, 3, SORT_DESC);
39
40
        if (!isset($timings[$logId])) {
41
            throw new HttpException(404, 'Log message not found.');
42
        }
43
44
        $request  = unserialize($timings[$logId][1]);
45
        $time     = microtime(true);
46
        $response = $request->send();
47
        $time     = microtime(true) - $time;
48
49
        Yii::$app->response->format = Response::FORMAT_JSON;
50
51
        return [
52
            'time'   => sprintf('%.1f ms', $time * 1000),
53
            'result' => $response->getData(),
54
        ];
55
    }
56
}
57