AdminUser::saveAdminUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 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\Framework\Exception\LocalizedException;
25
use Magento\User\Model\ResourceModel\User as UserResourceModel;
26
use Magento\User\Model\User;
27
28
class AdminUser
29
{
30
    /**
31
     * @var Session
32
     */
33
    private $adminSession;
34
    /**
35
     * @var UserResourceModel
36
     */
37
    private $resourceModel;
38
39
    /**
40
     * AdminUser constructor.
41
     *
42
     * @param Session           $adminSession
43
     * @param UserResourceModel $resourceModel
44
     */
45 18
    public function __construct(Session $adminSession, UserResourceModel $resourceModel)
46
    {
47 18
        $this->adminSession  = $adminSession;
48 18
        $this->resourceModel = $resourceModel;
49 18
    }
50
51
    /**
52
     * @return User
53
     * @throws \Exception
54
     */
55 15
    public function getAdminUser()
56
    {
57 15
        $adminUser = $this->adminSession->getData('user');
58 15
        if (!$adminUser instanceof User) {
59 2
            throw new LocalizedException(__("No admin user found"));
60
        }
61
62 13
        return $adminUser;
63
    }
64
65 13
    public function hasAdminUser()
66
    {
67 13
        return ($this->adminSession->hasData('user') === true);
68
    }
69
70 4
    public function saveAdminUser(User $user)
71
    {
72
73 4
        if ($user->isSaveAllowed() === true) {
74 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...
75
        }
76 4
    }
77
}
78