Passed
Push — master ( d12a36...d0848f )
by Ross
22:44
created

SaveBefore::addNewSecret()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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\Observer\AdminUser;
23
24
use Magento\Framework\Event\Observer;
25
use Magento\Framework\Event\ObserverInterface;
26
use Magento\User\Model\User;
27
use Rossmitchell\Twofactor\Model\Admin\Attribute\IsUsingTwoFactor;
28
use Rossmitchell\Twofactor\Model\Admin\Attribute\TwoFactorSecret;
29
use Rossmitchell\Twofactor\Model\Admin\Session;
30
use Rossmitchell\Twofactor\Model\GoogleTwoFactor\Secret;
31
use Rossmitchell\Twofactor\Model\Verification\IsVerified;
32
33
class SaveBefore implements ObserverInterface
34
{
35
    /**
36
     * @var IsUsingTwoFactor
37
     */
38
    private $isUsingTwoFactor;
39
    /**
40
     * @var TwoFactorSecret
41
     */
42
    private $twoFactorSecret;
43
    /**
44
     * @var Secret
45
     */
46
    private $secret;
47
    /**
48
     * @var Session
49
     */
50
    private $session;
51
    /**
52
     * @var IsVerified
53
     */
54
    private $isVerified;
55
56
    /**
57
     * SaveBefore constructor.
58
     *
59
     * @param IsUsingTwoFactor $isUsingTwoFactor
60
     * @param TwoFactorSecret  $twoFactorSecret
61
     * @param Secret           $secret
62
     * @param Session          $session
63
     * @param IsVerified       $isVerified
64
     */
65
    public function __construct(
66
        IsUsingTwoFactor $isUsingTwoFactor,
67
        TwoFactorSecret $twoFactorSecret,
68
        Secret $secret,
69
        Session $session,
70
        IsVerified $isVerified
71
    ) {
72
        $this->isUsingTwoFactor = $isUsingTwoFactor;
73
        $this->twoFactorSecret  = $twoFactorSecret;
74
        $this->secret           = $secret;
75
        $this->session          = $session;
76
        $this->isVerified       = $isVerified;
77
    }
78
79
    /**
80
     * @param Observer $observer
81
     *
82
     * @return void
83
     */
84 4
    public function execute(Observer $observer)
85
    {
86 4
        $adminUser = $observer->getEvent()->getData('data_object');
87 4
        if ($this->isSetToUseTwoFactor($adminUser) === false) {
88 2
            return;
89
        }
90
91 2
        if ($this->alreadyHasASecret($adminUser) === true) {
92 1
            return;
93
        }
94
95 1
        $this->addNewSecret($adminUser);
96 1
        $this->markAsValidated();
97 1
    }
98
99 4
    private function isSetToUseTwoFactor(User $user)
100
    {
101 4
        return ($this->isUsingTwoFactor->getValue($user) === true);
102
    }
103
104 2
    private function alreadyHasASecret(User $user)
105
    {
106 2
        return ($this->twoFactorSecret->getValue($user) !== null);
107
    }
108
109 1
    private function addNewSecret(User $user)
110
    {
111 1
        $secret = $this->secret->generateSecret();
112 1
        $this->twoFactorSecret->setValue($user, $secret);
113 1
    }
114
115 1
    private function markAsValidated()
116
    {
117 1
        $this->isVerified->setIsVerified($this->session);
118 1
    }
119
}
120