Completed
Push — 17.x ( 0c4704...c4b5cb )
by Tim
04:58
created

AttributeSetNamesValidatorCallback::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 15
cp 0
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Callbacks\AttributeSetNamesValidatorCallback
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 2019 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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Callbacks;
22
23
use TechDivision\Import\Attribute\Utils\ColumnKeys;
24
use TechDivision\Import\Callbacks\IndexedArrayValidatorCallback;
25
26
/**
27
 * A callback implementation that validates the a list of attribute set names.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2019 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-attribute
33
 * @link      http://www.techdivision.com
34
 */
35
class AttributeSetNamesValidatorCallback extends IndexedArrayValidatorCallback
36
{
37
38
    /**
39
     * Will be invoked by the observer it has been registered for.
40
     *
41
     * @param string|null $attributeCode  The code of the attribute that has to be validated
42
     * @param string|null $attributeValue The attribute value to be validated
43
     *
44
     * @return mixed The modified value
45
     */
46
    public function handle($attributeCode = null, $attributeValue = null)
47
    {
48
49
        // explode the values and query whether or not an empty value is allowed
50
        if ($this->isNullable($values = $this->getSubject()->explode($attributeValue))) {
51
            return;
52
        }
53
54
        // load the validations for the column
55
        $validations = $this->getValidations($this->getSubject()->getValue(ColumnKeys::ENTITY_TYPE_CODE));
56
57
        // iterate over the values and validate them
58
        foreach ($values as $value) {
59
            // query whether or not the value is valid
60
            if (in_array($value, $validations)) {
61
                continue;
62
            }
63
64
            // throw an exception if the value is NOT in the array
65
            throw new \InvalidArgumentException(
66
                sprintf('Found invalid value "%s" for column "%s"', $value, $attributeCode)
67
            );
68
        }
69
    }
70
}
71