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

Customer::getCustomerFromSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 9
cp 0.6667
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2.1481
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 27
    public function __construct(CustomerRepositoryInterface $customerRepository, Session $customerSession)
49
    {
50 27
        $this->customerRepository = $customerRepository;
51 27
        $this->customerSession    = $customerSession;
52 27
    }
53
54
    /**
55
     * Returns the customer model from the session. Will return false if there is no customer
56
     *
57
     * @return CustomerInterface|false
58
     */
59 18
    public function getCustomer()
60
    {
61 18
        if (null === $this->customer) {
62 18
            $this->customer = $this->getCustomerFromSession();
63
        }
64
65 18
        return $this->customer;
66
    }
67
68 15
    public function getCustomerById($customerId)
69
    {
70 15
        return $this->customerRepository->getById($customerId);
71
    }
72
73
    /**
74
     * @return bool|CustomerInterface
75
     */
76 18
    private function getCustomerFromSession()
77
    {
78 18
        $customerId = $this->customerSession->getData('customer_id');
79 18
        $customer   = false;
80
81 18
        if (null !== $customerId) {
82 15
            $customer = $this->getCustomerById($customerId);
83
        }
84
85 18
        return $customer;
86
    }
87
}
88