RequestPanel::saveCliRequest()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 23.2584

Importance

Changes 0
Metric Value
cc 7
nc 1
nop 0
dl 0
loc 19
ccs 4
cts 13
cp 0.3076
crap 23.2584
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace bedezign\yii2\audit\panels;
4
5
use bedezign\yii2\audit\components\panels\DataStoragePanelTrait;
6
use Yii;
7
use yii\base\InlineAction;
8
use yii\helpers\Inflector;
9
10
/**
11
 * RequestPanel
12
 * @package bedezign\yii2\audit\panels
13
 */
14
class RequestPanel extends \yii\debug\panels\RequestPanel
15
{
16
    use DataStoragePanelTrait;
17
18
    /**
19
     * @var array
20
     */
21
    public $ignoreKeys = [];
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 3
    public function getSummary()
27
    {
28 3
        return Yii::$app->view->render('panels/request/summary', ['panel' => $this]);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 57
    public function getDetail()
35
    {
36 57
        return Yii::$app->view->render('panels/request/detail', ['panel' => $this]);
37
    }
38
39 57
    /**
40
     * @inheritdoc
41
     */
42
    public function save()
43
    {
44
        if (Yii::$app->request instanceof \yii\console\Request) {
45 57
            return $this->saveCliRequest();
46
        }
47
        return $this->cleanData(parent::save());
48
    }
49
50
    /**
51
     * @return array
52
     */
53 57
    protected function saveCliRequest()
54
    {
55
        return $this->cleanData([
56
            'flashes' => $this->getFlashes(),
57
            'statusCode' => 0,
58
            'requestHeaders' => [],
59
            'responseHeaders' => [],
60
            'route' => $this->getRoute(),
61
            'action' => $this->getAction(),
62
            'actionParams' => Yii::$app->request->params,
63
            'requestBody' => [],
64
            'SERVER' => empty($_SERVER) ? [] : $_SERVER,
65
            'GET' => empty($_GET) ? [] : $_GET,
66
            'POST' => empty($_POST) ? [] : $_POST,
67
            'COOKIE' => empty($_COOKIE) ? [] : $_COOKIE,
68 6
            'FILES' => empty($_FILES) ? [] : $_FILES,
69
            'SESSION' => empty($_SESSION) ? [] : $_SESSION,
70 6
        ]);
71 6
    }
72 3
73
    /**
74 3
     * @return null|string
75
     */
76
    protected function getAction()
77
    {
78
        if (Yii::$app->requestedAction) {
79
            if (Yii::$app->requestedAction instanceof InlineAction) {
80
                return get_class(Yii::$app->requestedAction->controller) . '::' . Yii::$app->requestedAction->actionMethod . '()';
81
            }
82 6
            return get_class(Yii::$app->requestedAction) . '::run()';
83
        }
84 6
        return null;
85 3
    }
86
87 3
    /**
88
     * @return string
89
     */
90
    protected function getRoute()
91
    {
92
        if (Yii::$app->requestedAction) {
93
            return Inflector::camel2id(Yii::$app->requestedAction->getUniqueId());
94 57
        }
95
        return Yii::$app->requestedRoute;
96 57
    }
97 57
98
    /**
99
     * @param array $data
100 57
     * @return array
101 57
     */
102
    protected function cleanData($data)
103
    {
104
        foreach ($data as $k => $v) {
105
            if (in_array($k, $this->ignoreKeys)) {
106
                $data[$k] = null;
107
            }
108
        }
109
        return $data;
110
    }
111
112
}
113