Passed
Push — master ( 492849...8ad597 )
by Ross
02:52
created

QRCode::shouldQRCodeBeDisplayed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * A two factor authentication module that protects both the admin and customer logins
4
 * Copyright (C) 2017  Ross Mitchell
5
 *
6
 * This file is part of Rossmitchell/Twofactor.
7
 *
8
 * Rossmitchell/Twofactor is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace Rossmitchell\Twofactor\Block\Adminhtml\System\Account;
23
24
use Magento\Framework\View\Element\Template;
25
use Magento\User\Model\User;
26
use Rossmitchell\Twofactor\Model\Admin\AdminUser;
27
use Rossmitchell\Twofactor\Model\Admin\Attribute\TwoFactorSecret;
28
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\QRCode as GoogleQRCode;
29
30
class QRCode extends Template
31
{
32
    /**
33
     * @var AdminUser
34
     */
35
    private $adminUser;
36
    /**
37
     * @var TwoFactorSecret
38
     */
39
    private $twoFactorSecret;
40
    /**
41
     * @var GoogleQRCode
42
     */
43
    private $qRCode;
44
45
    /**
46
     * QRCode constructor.
47
     *
48
     * @param Template\Context $context
49
     * @param AdminUser        $adminUser
50
     * @param TwoFactorSecret  $twoFactorSecret
51
     * @param GoogleQRCode     $qRCode
52
     * @param array            $data
53
     */
54
    public function __construct(
55
        Template\Context $context,
56
        AdminUser $adminUser,
57
        TwoFactorSecret $twoFactorSecret,
58
        GoogleQRCode $qRCode,
59
        array $data = []
60
    ) {
61
        parent::__construct($context, $data);
62
        $this->adminUser = $adminUser;
63
        $this->twoFactorSecret = $twoFactorSecret;
64
        $this->qRCode = $qRCode;
65
    }
66
67
    public function getAdminUser()
68
    {
69
        return $this->adminUser->getAdminUser();
70
    }
71
72
    public function shouldQRCodeBeDisplayed(User $adminUser)
73
    {
74
        if ($this->getSecret($adminUser) === null) {
75
            return false;
76
        }
77
    }
78
79
    public function getQRCode(User $adminUser)
80
    {
81
        $secret = $this->getSecret($adminUser);
82
        return $this->qRCode->generateQRCode('Test Company', $adminUser->getEmail(), $secret);
83
    }
84
85
    private function getSecret(User $adminUser)
86
    {
87
        return $this->twoFactorSecret->getValue($adminUser);
88
    }
89
}
90