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

AdminUser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 2
dl 0
loc 50
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAdminUser() 0 9 2
A hasAdminUser() 0 4 1
A saveAdminUser() 0 7 2
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\Model\Admin;
23
24
use Magento\User\Model\User;
25
26
class AdminUser
27
{
28
    /**
29
     * @var Session
30
     */
31
    private $adminSession;
32
    /**
33
     * @var \Magento\User\Model\ResourceModel\User
34
     */
35
    private $resorceModel;
36
37
    /**
38
     * AdminUser constructor.
39
     *
40
     * @param Session                                $adminSession
41
     * @param \Magento\User\Model\ResourceModel\User $resourceModel
42
     */
43 18
    public function __construct(Session $adminSession, \Magento\User\Model\ResourceModel\User $resourceModel)
44
    {
45 18
        $this->adminSession = $adminSession;
46 18
        $this->resorceModel = $resourceModel;
47 18
    }
48
49
    /**
50
     * @return User
51
     * @throws \Exception
52
     */
53 15
    public function getAdminUser()
54
    {
55 15
        $adminUser = $this->adminSession->getData('user');
56 15
        if (!$adminUser instanceof User) {
57 2
            throw new \Exception("No admin user found");
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
58
        }
59
60 13
        return $adminUser;
61
    }
62
63 13
    public function hasAdminUser()
64
    {
65 13
        return ($this->adminSession->hasData('user') === true);
66
    }
67
68 4
    public function saveAdminUser(User $user)
69
    {
70
71 4
        if ($user->isSaveAllowed() === true) {
72 4
            $this->resorceModel->save($user->save());
0 ignored issues
show
Deprecated Code introduced by
The method Magento\Framework\Model\AbstractModel::save() has been deprecated.

This method has been deprecated.

Loading history...
73
        }
74 4
    }
75
}
76