Completed
Push — master ( f65280...92c5a8 )
by Andrii
21:30 queued 06:36
created

BackLinkWidget::registerJs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace hiqdev\yii2\mfa\widgets;
4
5
6
use Yii;
7
use yii\base\Widget;
8
use yii\helpers\Html;
9
10
/**
11
 * Class BackTOTPLinkWidget
12
 * @package hiqdev\yii2\mfa\widgets
13
 */
14
class BackLinkWidget extends Widget
15
{
16
    private $options = [
17
        'id'    => 'back-link',
18
        'class' => 'text-secondary',
19
    ];
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function run()
25
    {
26
        $this->registerJs();
27
28
        $backLink = HTML::a(Yii::t('mfa', 'Back'), '#', $this->options);
29
30
        return "<div style='margin-top: 15px;'>$backLink</div>";
31
    }
32
33
    private function registerJs()
34
    {
35
        $referrer = Yii::$app->request->referrer;
36
37
        $this->view->registerJs(/** @lang JavaScript */"
38
            $('#back-link').on('click', () => {
39
                const phpReferrer = '{$referrer}';
40
                
41
                (phpReferrer === '') ? window.history.go(-1) : window.location.href = phpReferrer;
42
            });
43
        ");
44
    }
45
}
46