Getter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasValue() 0 4 1
A getDataFromInterface() 0 9 2
A getDataFromCustomerModel() 0 4 1
A getValue() 0 16 4
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\Attribute;
23
24
use Magento\Customer\Api\Data\CustomerInterface;
25
use Magento\Customer\Model\Customer;
26
use Magento\Framework\Exception\InputException;
27
28
class Getter
29
{
30 22
    public function getValue($object, $attributeCode)
31
    {
32 22
        if (!is_object($object)) {
33 1
            throw InputException::invalidFieldValue('object', 'false');
34
        }
35
36 21
        if ($object instanceof CustomerInterface) {
37 18
            return $this->getDataFromInterface($object, $attributeCode);
38
        }
39
40 6
        if ($object instanceof Customer) {
41 5
            return $this->getDataFromCustomerModel($object, $attributeCode);
42
        }
43
44 1
        throw InputException::invalidFieldValue('object', get_class($object));
45
    }
46
47 19
    public function hasValue($object, $attributeCode)
48
    {
49 19
        return ($this->getValue($object, $attributeCode) !== null);
50
    }
51
52 18
    private function getDataFromInterface(CustomerInterface $customer, $attributeCode)
53
    {
54 18
        $option = $customer->getCustomAttribute($attributeCode);
55 18
        if (null === $option) {
56 2
            return null;
57
        }
58
59 17
        return $option->getValue();
60
    }
61
62 5
    private function getDataFromCustomerModel(Customer $customer, $attributeCode)
63
    {
64 5
        return $customer->getData($attributeCode);
65
    }
66
}
67