Passed
Push — master ( a8ac3c...b503e4 )
by Klochok
11:06
created

Timing::getRunLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
/**
3
 * ActiveRecord for API
4
 *
5
 * @link      https://github.com/hiqdev/yii2-hiart
6
 * @package   yii2-hiart
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\hiart\debug;
12
13
use hiqdev\hiart\helpers\Request2Curl;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
use yii\helpers\Json;
17
use yii\helpers\Url;
18
use yii\web\JsExpression;
19
20
class Timing
21
{
22
    /**
23
     * @var DebugPanel
24
     */
25
    protected $panel;
26
27
    protected $logId;
28
29
    protected $duration;
30
31
    protected $traces;
32
33
    protected $request;
34
35
    public function __construct(DebugPanel $panel, $logId)
36
    {
37
        $this->panel = $panel;
38
        $this->logId = $logId;
39
    }
40
41
    public static function buildAll(DebugPanel $panel)
42
    {
43
        $rawTimings = $panel->getTimings();
44
        ArrayHelper::multisort($rawTimings, 3, SORT_DESC);
45
46
        $timings = [];
47
        foreach ($rawTimings as $logId => $rawTiming) {
48
            $timings[] = static::buildOne($panel, $logId, $rawTiming);
49
        }
50
51
        return $timings;
52
    }
53
54
    public static function buildOne($panel, $logId, $rawTiming)
55
    {
56
        $new = new static($panel, $logId);
57
        $new->updateFromRaw($rawTiming);
58
59
        return $new;
60
    }
61
62
    public function updateFromRaw($rawTiming)
63
    {
64
        $this->request = unserialize($rawTiming[1]);
65
        $this->duration = $rawTiming[3];
66
        $this->traces = $rawTiming[4];
67
    }
68
69
    public function getLogId()
70
    {
71
        return $this->logId;
72
    }
73
74
    public function getMethod()
75
    {
76
        return $this->request->getMethod();
77
    }
78
79
    public function getUrlEncoded()
80
    {
81
        return Html::encode($this->request->getFullUri());
82
    }
83
84
    public function getBodyEncoded()
85
    {
86
        return Html::encode($this->request->getBody());
87
    }
88
89
    public function getHeaders(): array
90
    {
91
        $headers = [];
92
        foreach ($this->request->getHeaders() as $header => $value) {
93
            $headers[] = Html::encode("$header: $value");
94
        }
95
96
        return $headers;
97
    }
98
99
    public function getDuration()
100
    {
101
        return sprintf('%.1f ms', $this->duration * 1000);
102
    }
103
104
    public function getTrace()
105
    {
106
        $result = '';
107
        if (!empty($this->traces)) {
108
            $result .= Html::ul($this->traces, [
109
                'class' => 'trace',
110
                'item' => function ($trace) {
111
                    return '<li>' . $this->panel->getTraceLine($trace) . '</li>';
112
                },
113
            ]);
114
        }
115
116
        return $result;
117
    }
118
119
    public function getRunLink()
120
    {
121
        $ajaxUrl = Url::to(['hiart-query', 'logId' => $this->logId, 'tag' => $this->panel->tag]);
122
123
        return Html::a('run query', $ajaxUrl, [
124
            'class' => 'hiart-link',
125
            'data' => ['id' => $this->logId],
126
        ]);
127
    }
128
129
    public function getNewTabLink()
130
    {
131
        $uri = rtrim($this->request->getFullUri(), '?');
132
        $sign = strpos($uri, '?') === false ? '?' : '&';
133
        $newTabUrl = rtrim($uri, '&') . $sign . $this->request->getBody();
134
135
        return Html::a('to new tab', $newTabUrl, ['target' => '_blank']);
136
    }
137
138
    public function getCopyAsCurlLink(): string
139
    {
140
        $curl = Json::htmlEncode((string)(new Request2Curl($this->request)));
141
142
        return Html::a('copy as cURL', '#', [
143
            'onclick' => new JsExpression("
144
                (function () {
145
                    if (navigator.clipboard && window.isSecureContext) {
146
                        return navigator.clipboard.writeText($curl);
147
                    } else {
148
                        let textArea = document.createElement('textarea');
149
                        textArea.value = $curl;
150
                        textArea.style.position = 'fixed';
151
                        textArea.style.left = '-999999px';
152
                        textArea.style.top = '-999999px';
153
                        document.body.appendChild(textArea);
154
                        textArea.focus();
155
                        textArea.select();
156
                        return new Promise((res, rej) => {
157
                            document.execCommand('copy') ? res() : rej();
158
                            textArea.remove();
159
                        });
160
                    }
161
                })();
162
                return false;
163
            "),
164
        ]);
165
    }
166
}
167