Completed
Push — master ( 50a205...4eae55 )
by Ross
51:48 queued 20s
created

AbstractTestClass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
c 1
b 1
f 0
lcom 0
cbo 1
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormKey() 0 4 1
A login() 0 12 2
A createObject() 0 10 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\Tests\Integration\Abstracts;
23
24
use Magento\Customer\Model\Customer;
25
use Magento\Customer\Model\Session;
26
use Magento\Framework\Data\Form\FormKey;
27
use Magento\TestFramework\TestCase\AbstractController;
28
29
class AbstractTestClass extends AbstractController
30
{
31
32
    public function getFormKey()
33
    {
34
        return $this->createObject(FormKey::class)->getFormKey();
35
    }
36
37
    /**
38
     * Login the user
39
     *
40
     * @param string $customerEmail Customer to mark as logged in for the session
41
     *
42
     * @param int    $websiteId
43
     *
44
     * @return void
45
     * @throws \Exception
46
     */
47
    public function login($customerEmail, $websiteId = 1)
48
    {
49
        /** @var Session $session */
50
        $session = $this->createObject(Session::class, false);
51
        /** @var Customer $customer */
52
        $customer = $this->createObject(Customer::class);
53
        $customer->setWebsiteId($websiteId);
54
        $customerId = $customer->loadByEmail($customerEmail);
55
        if($session->loginById($customerId->getId()) === false) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
56
            throw new \Exception("Could not log customer in");
0 ignored issues
show
introduced by
Direct throw of Exception is discouraged. Use \Magento\Framework\Exception\LocalizedException instead.
Loading history...
57
        }
58
    }
59
60
    public function createObject($className, $new = true)
61
    {
62
        if ($new === true) {
63
            return $this->_objectManager->create($className);
0 ignored issues
show
introduced by
The direct use of ObjectManager is discouraged. Inject necessary dependencies via constructor.
Loading history...
64
        }
65
66
        return $this->_objectManager->get($className);
0 ignored issues
show
introduced by
The direct use of ObjectManager is discouraged. Inject necessary dependencies via constructor.
Loading history...
67
0 ignored issues
show
introduced by
Code must not contain multiple empty lines in a row; found 2 empty lines.
Loading history...
68
69
    }
0 ignored issues
show
Coding Style introduced by
Function closing brace must go on the next line following the body; found 2 blank lines before brace
Loading history...
70
}
71