Passed
Push — master ( d0848f...c51354 )
by Ross
27:06
created

Getter::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 6
cts 8
cp 0.75
rs 9.2
cc 4
eloc 8
nc 4
nop 2
crap 4.25
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 16
    public function getValue($object, $attributeCode)
31
    {
32 16
        if (!is_object($object)) {
33
            throw InputException::invalidFieldValue('object', 'false');
34
        }
35
36 16
        if ($object instanceof CustomerInterface) {
37 14
            return $this->getDataFromInterface($object, $attributeCode);
38
        }
39
40 5
        if ($object instanceof Customer) {
41 5
            return $this->getDataFromCustomerModel($object, $attributeCode);
42
        }
43
44
        throw InputException::invalidFieldValue('object', get_class($object));
45
    }
46
47 16
    public function hasValue($object, $attributeCode)
48
    {
49 16
        return ($this->getValue($object, $attributeCode) !== null);
50
    }
51
52 14
    private function getDataFromInterface(CustomerInterface $customer, $attributeCode)
53
    {
54 14
        $option = $customer->getCustomAttribute($attributeCode);
55 14
        if (null === $option) {
56 1
            return null;
57
        }
58
59 14
        return $option->getValue();
60
    }
61
62 5
    private function getDataFromCustomerModel(Customer $customer, $attributeCode)
63
    {
64 5
        return $customer->getData($attributeCode);
65
    }
66
}
67