Passed
Push — master ( 785691...679eaf )
by Ross
39:28
created

AdminUser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadData() 0 13 3
A rollBackData() 0 10 2
B verifyData() 0 17 5
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\Tests\Integration\FixtureLoader;
23
24
use Magento\User\Model\UserFactory;
25
26
class AdminUser extends AbstractLoader
27
{
28
29
    public function loadData()
30
    {
31
        $this->setSecureArea();
32
        /** @var UserFactory $userFactory */
33
        $userFactory = $this->createObject(UserFactory::class);
34
        foreach ($this->data as $userData) {
35
            $user = $userFactory->create();
36
            foreach ($userData as $key => $value) {
37
                $user->setData($key, $value);
38
            }
39
            $user->save();
0 ignored issues
show
introduced by
Model LSD method save() detected in loop
Loading history...
40
        }
41
    }
42
43
    public function rollBackData()
44
    {
45
        /** @var UserFactory $userFactory */
46
        $userFactory = $this->createObject(UserFactory::class);
47
        foreach ($this->data as $userData) {
48
            $user = $userFactory->create();
49
            $user->loadByUsername($userData['username']);
50
            $user->delete();
0 ignored issues
show
introduced by
Model LSD method delete() detected in loop
Loading history...
51
        }
52
    }
53
54
    public function verifyData()
55
    {
56
        foreach ($this->data as $userData) {
57
            if (isset($userData['user_id'])) {
58
                throw new \Exception('You can not set a user_id for an admin user - it prevents them saving');
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
59
            }
60
61
            $requiredAttributes = ['username', 'password'];
62
63
            foreach ($requiredAttributes as $attribute) {
64
                if (!isset($userData[$attribute])) {
65
                    throw new \Exception("You must set a value for $attribute");
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
66
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
67
                }
68
            }
69
        }
70
    }
71
}
72