Completed
Push — 19.x ( 5424e9...4eb75f )
by Tim
03:53
created

RmaValidatorCallback   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 58
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 28 4
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
     * Will be invoked by the observer it has been registered for.
58
     *
59
     * @param string|null $attributeCode  The code of the attribute that has to be validated
60
     * @param string|null $attributeValue The attribute value to be validated
61
     *
62
     * @return void
63
     * @throws \InvalidArgumentException Is thrown, if either the validator has been bound to an invalid column or an invalid value has been found
64
     */
65
    public function handle($attributeCode = null, $attributeValue = null)
66
    {
67
68
        // query whether or not the passed attribute code matches and the value is a valid RMA key
69
        if (strtolower($attributeCode) === ColumnKeys::IS_RETURNABLE && $this->rmaKeys->isValid($attributeValue)) {
70
            return;
71
        }
72
73
        // throw an exception if the validator has bound to an invalid column
74
        if (strtolower($attributeCode) !== ColumnKeys::IS_RETURNABLE) {
75
            throw new \InvalidArgumentException(
76
                sprintf(
77
                    'Tried to bind RMA validator on invalid column "%s"',
78
                    $attributeCode
79
                )
80
            );
81
        }
82
83
        // throw an exception if the value is NOT a valid RMA key
84
        throw new \InvalidArgumentException(
85
            sprintf(
86
                'Found invalid RMA key "%s" for column "%s" (must be one of: "%s")',
87
                $attributeValue,
88
                $attributeCode,
89
                implode(', ', array_keys($this->rmaKeys->getAll()))
90
            )
91
        );
92
    }
93
}
94