Completed
Push — master ( 59b3e2...6dd6b9 )
by Andrii
15:38
created

DebugAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 24 2
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
        $db       = Yii::$app->get($request->getDbname());
46
        $time     = microtime(true);
47
        $response = $db->send($request);
48
        $time     = microtime(true) - $time;
49
50
        Yii::$app->response->format = Response::FORMAT_JSON;
51
52
        return [
53
            'time'   => sprintf('%.1f ms', $time * 1000),
54
            'result' => $response->getData(),
55
        ];
56
    }
57
}
58