Passed
Push — master ( b162a4...87a689 )
by Ross
03:05
created

Customer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCustomer() 0 8 2
A getCustomerFromSession() 0 11 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\Customer;
23
24
use Magento\Customer\Api\CustomerRepositoryInterface;
25
use Magento\Customer\Api\Data\CustomerInterface;
26
27
class Customer
28
{
29
    /**
30
     * @var CustomerRepositoryInterface
31
     */
32
    private $customerRepository;
33
    /**
34
     * @var Session
35
     */
36
    private $customerSession;
37
    /**
38
     * @var CustomerInterface|false
39
     */
40
    private $customer;
41
42
    /**
43
     * Getter constructor.
44
     *
45
     * @param CustomerRepositoryInterface $customerRepository
46
     * @param Session                     $customerSession
47
     */
48
    public function __construct(CustomerRepositoryInterface $customerRepository, Session $customerSession)
49
    {
50
        $this->customerRepository = $customerRepository;
51
        $this->customerSession    = $customerSession;
52
    }
53
54
    public function getCustomer()
55
    {
56
        if (null === $this->customer) {
57
            $this->customer = $this->getCustomerFromSession();
58
        }
59
60
        return $this->customer;
61
    }
62
63
    private function getCustomerFromSession()
64
    {
65
        $customerId = $this->customerSession->getData('customer_id');
66
        $customer   = false;
67
68
        if (null !== $customerId) {
69
            $customer = $this->customerRepository->getById($customerId);
70
        }
71
72
        return $customer;
73
    }
74
}
75