QrCodeWidget   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 65
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A run() 0 24 5
1
<?php
2
/**
3
 * @link https://github.com/vuongxuongminh/yii2-mfa
4
 * @copyright Copyright (c) 2019 Vuong Xuong Minh
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace vxm\mfa;
9
10
use yii\base\InvalidCallException;
11
use yii\base\Widget;
12
use yii\helpers\Html;
13
14
/**
15
 * Class QrCodeWidget provide a qr code for authenticator like google authenticator of current logged in user.
16
 *
17
 * @author Vuong Minh <[email protected]>
18
 * @since 1.0.0
19
 */
20
class QrCodeWidget extends Widget
21
{
22
23
    use EnsureUserBehaviorAttachedTrait;
24
25
    /**
26
     * @var array HTML img tag attributes.
27
     */
28
    public $options = [];
29
30
    /**
31
     * @var string an issuer will show in authenticator application. If not set an application name will be use to set by default.
32
     */
33
    public $issuer;
34
35
    /**
36
     * @var string a label will show in authenticator application.
37
     */
38
    public $label;
39
40
    /**
41
     * @var string a image will show in authenticator application.
42
     */
43
    public $image;
44
45
    /**
46
     * @inheritDoc
47
     */
48 3
    public function init()
49
    {
50 3
        $this->ensureUserBehaviorAttached();
51
52 3
        parent::init();
53 3
    }
54
55
    /**
56
     * @inheritDoc
57
     * @throws InvalidCallException
58
     */
59 3
    public function run()
60
    {
61 3
        $params = [];
62
63 3
        if ($this->issuer) {
64 1
            $params['issuer'] = $this->issuer;
65
        }
66
67 3
        if ($this->label) {
68 1
            $params['label'] = $this->label;
69
        }
70
71 3
        if ($this->image) {
72 1
            $params['image'] = $this->image;
73
        }
74
75 3
        $uri = $this->user->getQrCodeUri($params);
76
77 2
        if ($uri) {
78 1
            return Html::img($uri, $this->options);
79
        } else {
80 1
            throw new InvalidCallException('Current user is guest, can not render qr code!');
81
        }
82
    }
83
84
}
85