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

Timing::getBodyEncoded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 hiqdev\hiart\Request;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
use yii\helpers\Url;
17
18
class Timing
19
{
20
    protected $panel;
21
22
    protected $logId;
23
    protected $duration;
24
    protected $traces;
25
    protected $request;
26
27
    public function __construct(DebugPanel $panel, $logId)
28
    {
29
        $this->panel = $panel;
30
        $this->logId = $logId;
31
    }
32
33
    public static function buildAll(DebugPanel $panel)
34
    {
35
        $rawTimings = $panel->getTimings();
36
        ArrayHelper::multisort($rawTimings, 3, SORT_DESC);
37
38
        $timings = [];
39
        foreach ($rawTimings as $logId => $rawTiming) {
40
            $timings[] = static::buildOne($panel, $logId, $rawTiming);
41
        }
42
43
        return $timings;
44
    }
45
46
    public static function buildOne($panel, $logId, $rawTiming)
47
    {
48
        $new = new static($panel, $logId);
49
        $new->updateFromRaw($rawTiming);
50
51
        return $new;
52
    }
53
54
    public function updateFromRaw($rawTiming)
55
    {
56
        $this->request  = unserialize($rawTiming[1]);
57
        $this->duration = $rawTiming[3];
58
        $this->traces   = $rawTiming[4];
59
    }
60
61
    public function getLogId()
62
    {
63
        return $this->logId;
64
    }
65
66
    public function getMethod()
67
    {
68
        return $this->request->getMethod();
69
    }
70
71
    public function getUrlEncoded()
72
    {
73
        return Html::encode($this->request->getFullUri());
74
    }
75
76
    public function getBodyEncoded()
77
    {
78
        return Html::encode($this->request->getBody());
79
    }
80
81
    public function getDuration()
82
    {
83
        return sprintf('%.1f ms', $this->duration * 1000);
84
    }
85
86
    public function getTrace()
87
    {
88
        $result = '';
89
        if (!empty($this->traces)) {
90
            $result .= Html::ul($this->traces, [
91
                'class' => 'trace',
92
                'item' => function ($trace) {
93
                    return "<li>{$trace['file']}({$trace['line']})</li>";
94
                },
95
            ]);
96
        }
97
98
        return $result;
99
    }
100
101
    public function getRunLink()
102
    {
103
        $ajaxUrl = Url::to(['hiart-query', 'logId' => $this->logId, 'tag' => $this->panel->tag]);
104
105
        return Html::a('run query', $ajaxUrl, [
106
            'class' => 'hiart-link',
107
            'data' => ['id' => $this->logId],
108
        ]);
109
    }
110
111
    public function getNewTabLink()
112
    {
113
        $uri = $this->request->getFullUri();
114
        $sign = strpos($uri, '?') === false ? '?' : '';
115
        $newTabUrl = rtrim($uri, '&') . $sign . $this->request->getBody();
116
117
        return Html::a('to new tab', $newTabUrl, ['target' => '_blank']);
118
    }
119
}
120