Completed
Push — 23.x ( 2e2714 )
by Tim
02:05
created

RmaValidatorCallback::isNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Ee\Callbacks\RmaValidatorCallback
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-ee
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Ee\Callbacks;
22
23
use TechDivision\Import\Callbacks\CallbackInterface;
24
use TechDivision\Import\Product\Ee\Utils\RmaKeysInterface;
25
use TechDivision\Import\Product\Ee\Utils\ColumnKeys;
26
27
/**
28
 * A callback implementation that validates the passed RMA keys.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2020 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-ee
34
 * @link      http://www.techdivision.com
35
 */
36
class RmaValidatorCallback implements CallbackInterface
37
{
38
39
    /**
40
     * The RMA keys utility instance.
41
     *
42
     * @var \TechDivision\Import\Product\Ee\Utils\RmaKeysInterface
43
     */
44
    protected $rmaKeys = null;
45
46
    /**
47
     * Initializes the callback with the RMA keys utility instance.
48
     *
49
     * @param \TechDivision\Import\Product\Ee\Utils\RmaKeysInterface $rmaKeys The RMA keys utility instance
50
     */
51
    public function __construct(RmaKeysInterface $rmaKeys)
52
    {
53
        $this->rmaKeys = $rmaKeys;
54
    }
55
56
    /**
57
     * Query whether or not the passed value IS empty and empty values are allowed.
58
     *
59
     * @param string $attributeValue The attribute value to query for
60
     *
61
     * @return boolean TRUE if empty values are allowed and the passed value IS empty
62
     */
63
    protected function isNullable($attributeValue)
64
    {
65
        return $attributeValue === '' || $attributeValue === null;
66
    }
67
68
    /**
69
     * Will be invoked by the observer it has been registered for.
70
     *
71
     * @param string|null $attributeCode  The code of the attribute that has to be validated
72
     * @param string|null $attributeValue The attribute value to be validated
73
     *
74
     * @return void
75
     * @throws \InvalidArgumentException Is thrown, if either the validator has been bound to an invalid column or an invalid value has been found
76
     */
77
    public function handle($attributeCode = null, $attributeValue = null)
78
    {
79
80
        // query whether or not the passed attribute code matches and the value is a valid RMA key
81
        if (strtolower($attributeCode) === ColumnKeys::IS_RETURNABLE && ($this->isNullable($attributeValue) || $this->rmaKeys->isValid($attributeValue))) {
82
            return;
83
        }
84
85
        // throw an exception if the validator has bound to an invalid column
86
        if (strtolower($attributeCode) !== ColumnKeys::IS_RETURNABLE) {
87
            throw new \InvalidArgumentException(
88
                sprintf(
89
                    'Tried to bind RMA validator on invalid column "%s"',
90
                    $attributeCode
91
                )
92
            );
93
        }
94
95
        // throw an exception if the value is NOT a valid RMA key
96
        throw new \InvalidArgumentException(
97
            sprintf(
98
                'Found invalid RMA key "%s" for column "%s" (must be one of: "%s")',
99
                $attributeValue,
100
                $attributeCode,
101
                implode(', ', array_keys($this->rmaKeys->getAll()))
102
            )
103
        );
104
    }
105
}
106