Completed
Push — master ( 7cc303...417512 )
by Klochok
04:06
created

BackButton::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 0
crap 6
1
<?php
2
3
namespace hipanel\widgets;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
9
class BackButton extends Widget
10
{
11
    /**
12
     * Html options
13
     * @var array
14
     */
15
    public $options = [
16
        'class' => 'btn btn-default',
17
    ];
18
19
    /**
20
     * @var string
21
     */
22
    public $selectorClass = 'btn-cancel';
23
24
    /**
25
     * @var string
26
     */
27
    public $label;
28
29
    public function run()
30
    {
31
        $curr = $this->getCurrentUrl();
32
        $ref = $this->getReferrerUrl();
33
        if ($curr === $ref) {
34
            Yii::$app->session[$this->getIdentifier()] = Yii::$app->session[$this->getIdentifier()] + 1;
35
        } else {
36
            Yii::$app->session[$this->getIdentifier()] = 1;
37
        }
38
        $this->registerClientScript();
39
        Html::addCssClass($this->options, $this->selectorClass);
40
41
        return Html::button($this->getLabel(), $this->options);
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getLabel()
48
    {
49
        return $this->label !== null ? $this->label : Yii::t('hipanel', 'Cancel');
50
    }
51
52
    protected function registerClientScript()
53
    {
54
        $this->getView()->registerJs(";$('.{$this->selectorClass}').on('click', function () {window.history.go({$this->getStep()});});");
55
    }
56
57
    /**
58
     * @return int
59
     */
60
    protected function getStep()
61
    {
62
        return -Yii::$app->session[$this->getIdentifier()];
63
    }
64
65
    protected function getIdentifier()
66
    {
67
        return self::class;
68
    }
69
70
    protected function getCurrentUrl()
71
    {
72
        return Yii::$app->request->absoluteUrl;
73
    }
74
75
    protected function getReferrerUrl()
76
    {
77
        $out = null;
0 ignored issues
show
Unused Code introduced by
$out is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
        $referrer = Yii::$app->request->referrer;
79
        $position = strpos($referrer, '?');
80
        if ($position !== false) {
81
            $out = substr($referrer, 0, $position);
82
        } else {
83
            $out = $referrer;
84
        }
85
86
        return $out;
87
    }
88
}
89