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

BackTOTPLinkWidget   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 8 1
A registerJs() 0 12 1
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