Completed
Push — master ( 70bc12...725ad3 )
by Ross
35:29
created

Getter::getDataFromInterface()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2.032
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 18
    public function getValue($object, $attributeCode)
31
    {
32 18
        if (!is_object($object)) {
33
            throw InputException::invalidFieldValue('object', 'false');
34
        }
35
36 18
        if ($object instanceof CustomerInterface) {
37 18
            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 2
    public function hasValue($object, $attributeCode)
48
    {
49 2
        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
            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