|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A Magento 2 module named Rossmitchell/Twofactor |
|
4
|
|
|
* Copyright (C) 2017 |
|
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\Model\Api\ResponseBuilder; |
|
23
|
|
|
|
|
24
|
|
|
use Magento\Customer\Api\Data\CustomerInterface; |
|
25
|
|
|
use Rossmitchell\Twofactor\Api\Response\GetQrCodeInterfaceFactory; |
|
26
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\IsUsingTwoFactor; |
|
27
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Attribute\TwoFactorSecret; |
|
28
|
|
|
use Rossmitchell\Twofactor\Model\Customer\Customer; |
|
29
|
|
|
use Rossmitchell\Twofactor\Model\Customer\GetQrCode as GetQrCodeFetcher; |
|
30
|
|
|
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\QRCode; |
|
31
|
|
|
|
|
32
|
|
|
class GetQrCode |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var GetQrCodeInterfaceFactory |
|
36
|
|
|
*/ |
|
37
|
|
|
private $response; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var IsUsingTwoFactor |
|
40
|
|
|
*/ |
|
41
|
|
|
private $isUsingTwoFactor; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var TwoFactorSecret |
|
44
|
|
|
*/ |
|
45
|
|
|
private $twoFactorSecret; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var QRCode |
|
48
|
|
|
*/ |
|
49
|
|
|
private $qrCode; |
|
50
|
|
|
/** |
|
51
|
|
|
* @var GetQrCodeFetcher |
|
52
|
|
|
*/ |
|
53
|
|
|
private $getQrCode; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* GetQrCode constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param GetQrCodeInterfaceFactory $response |
|
59
|
|
|
* @param IsUsingTwoFactor $isUsingTwoFactor |
|
60
|
|
|
* @param TwoFactorSecret $twoFactorSecret |
|
61
|
|
|
* @param QRCode $qrCode |
|
62
|
|
|
* @param GetQrCodeFetcher $getQrCode |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct( |
|
65
|
|
|
GetQrCodeInterfaceFactory $response, |
|
66
|
|
|
IsUsingTwoFactor $isUsingTwoFactor, |
|
67
|
|
|
TwoFactorSecret $twoFactorSecret, |
|
68
|
|
|
QRCode $qrCode, |
|
69
|
|
|
GetQrCodeFetcher $getQrCode |
|
70
|
|
|
) { |
|
71
|
|
|
|
|
72
|
|
|
$this->response = $response; |
|
73
|
|
|
$this->isUsingTwoFactor = $isUsingTwoFactor; |
|
74
|
|
|
$this->twoFactorSecret = $twoFactorSecret; |
|
75
|
|
|
$this->qrCode = $qrCode; |
|
76
|
|
|
$this->getQrCode = $getQrCode; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function buildResponseForCustomer(CustomerInterface $customer) |
|
80
|
|
|
{ |
|
81
|
|
|
$response = $this->response->create(); |
|
82
|
|
|
$response->setEmail($customer->getEmail()); |
|
83
|
|
|
$isUsing = $this->isUsingTwoFactor($customer); |
|
84
|
|
|
$response->setIsUsingTwoFactor($isUsing); |
|
85
|
|
|
|
|
86
|
|
|
if ($isUsing === true) { |
|
87
|
|
|
$response->setQrCode($this->getQrCode->getQrCode($customer)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
View Code Duplication |
private function isUsingTwoFactor(CustomerInterface $customer) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$hasValue = $this->isUsingTwoFactor->hasValue($customer); |
|
96
|
|
|
if ($hasValue === false) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->isUsingTwoFactor->getValue($customer); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.