Completed
Pull Request — master (#8)
by
unknown
11:22
created

BackTOTPLinkWidget::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
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 BackTOTPLinkWidget extends Widget
15
{
16
    /**
17
     * @inheritDoc
18
     */
19
    public function run()
20
    {
21
        $this->registerJs();
22
23
        $backLink = HTML::a(Yii::t('mfa', 'Back'), '#', ['id' => 'back-link']);
24
25
        return "<div>$backLink</div>";
26
    }
27
28
    private function registerJs()
29
    {
30
        $referrer = Yii::$app->request->referrer;
31
32
        $this->view->registerJs(/** @lang JavaScript */"
33
            $('#back-link').on('click', () => {
34
                const phpReferrer = '{$referrer}';
35
                
36
                (phpReferrer === '') ? window.history.go(-1) : window.location.href = phpReferrer;
37
            });
38
        ");
39
    }
40
}
41