Completed
Push — master ( f58984...07dd6c )
by Ross
26:19
created

AdminUser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 65.22%

Importance

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

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\ResourceModel\User as UserResourceModel;
25
use Magento\User\Model\User;
26
27
class AdminUser
28
{
29
    /**
30
     * @var Session
31
     */
32
    private $adminSession;
33
    /**
34
     * @var UserResourceModel
35
     */
36
    private $resourceModel;
37
38
    /**
39
     * AdminUser constructor.
40
     *
41
     * @param Session           $adminSession
42
     * @param UserResourceModel $resourceModel
43
     */
44 18
    public function __construct(Session $adminSession, UserResourceModel $resourceModel)
45
    {
46 18
        $this->adminSession  = $adminSession;
47 18
        $this->resourceModel = $resourceModel;
48 18
    }
49
50
    /**
51
     * @return User
52
     * @throws \Exception
53
     */
54 15
    public function getAdminUser()
55
    {
56 15
        $adminUser = $this->adminSession->getData('user');
57 15
        if (!$adminUser instanceof User) {
58 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...
59
        }
60
61 13
        return $adminUser;
62
    }
63
64 13
    public function hasAdminUser()
65
    {
66 13
        return ($this->adminSession->hasData('user') === true);
67
    }
68
69 4
    public function saveAdminUser(User $user)
70
    {
71
72 4
        if ($user->isSaveAllowed() === true) {
73 4
            $this->resourceModel->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...
74
        }
75 4
    }
76
}
77