Completed
Push — master ( 725ad3...990c1d )
by Ross
38:00
created

Getter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 64.71%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 16 4
A getDataFromCustomerModel() 0 4 1
A hasValue() 0 4 1
A getDataFromInterface() 0 9 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\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 20
    public function getValue($object, $attributeCode)
31
    {
32 20
        if (!is_object($object)) {
33
            throw InputException::invalidFieldValue('object', 'false');
34
        }
35
36 20
        if ($object instanceof CustomerInterface) {
37 20
            return $this->getDataFromInterface($object, $attributeCode);
38
        }
39
40
        if ($object instanceof Customer) {
41
            return $this->getDataFromCustomerModel($object, $attributeCode);
42
        }
43
44
        throw InputException::invalidFieldValue('object', get_class($object));
45
    }
46
47 20
    public function hasValue($object, $attributeCode)
48
    {
49 20
        return ($this->getValue($object, $attributeCode) !== null);
50
    }
51
52 20
    private function getDataFromInterface(CustomerInterface $customer, $attributeCode)
53
    {
54 20
        $option = $customer->getCustomAttribute($attributeCode);
55 20
        if (null === $option) {
56 2
            return null;
57
        }
58
59 18
        return $option->getValue();
60
    }
61
62
    private function getDataFromCustomerModel(Customer $customer, $attributeCode)
63
    {
64
        return $customer->getData($attributeCode);
65
    }
66
}
67