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

AttributeGroupNamesValidatorCallback::handle()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 19
cp 0
rs 9.0808
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Callbacks\AttributeGroupNamesValidatorCallback
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-product
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 attribute group names in an attribute import.
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-product
33
 * @link      http://www.techdivision.com
34
 */
35
class AttributeGroupNamesValidatorCallback extends IndexedArrayValidatorCallback
36
{
37
38
    /**
39
     * Returns the validations for the attribute with the passed code.
40
     *
41
     * @param string|null $attributeCode The code of the attribute to return the validations for
42
     *
43
     * @return array The allowed values for the attribute with the passed code
44
     */
45
    protected function getValidations($attributeCode = null, $entityTypeCode = null)
46
    {
47
48
        // load the validations for the given entity type code
49
        $validations = parent::getValidations($entityTypeCode);
50
51
        // query whether or not validations for the passed attribute code has been specified
52
        if (isset($validations[$attributeCode])) {
53
            return $validations[$attributeCode];
54
        }
55
56
        // return an empty array, if NOT
57
        return array();
58
    }
59
60
    /**
61
     * Will be invoked by the observer it has been registered for.
62
     *
63
     * @param string|null $attributeCode  The code of the attribute that has to be validated
64
     * @param string|null $attributeValue The attribute value to be validated
65
     *
66
     * @return mixed The modified value
67
     */
68
    public function handle($attributeCode = null, $attributeValue = null)
69
    {
70
71
        // load the subject instance
72
        $subject = $this->getSubject();
73
74
        // explode the attribute group names
75
        if ($this->isNullable($attributeGroupNames = $subject->explode($attributeValue))) {
76
            return;
77
        }
78
79
        // load the attribute set names of attribute groups that has to be validated
80
        $attributeSetNames = $subject->getValue(ColumnKeys::ATTRIBUTE_SET_NAME, array(), array($subject, 'explode'));
81
82
        // iterate over the attribute set names to load the available attribute group names therefore
83
        foreach ($attributeSetNames as $attributeSetName) {
84
            // load the validations for the attribute set with the given name
85
            $validations = $this->getValidations($attributeSetName, $subject->getValue(ColumnKeys::ENTITY_TYPE_CODE));
86
87
            // iterate over the attribute group names and validate them
88
            foreach ($attributeGroupNames as $attributeGroupName) {
89
                // query whether or not the value is valid
90
                if (in_array($attributeGroupName, $validations)) {
91
                    continue;
92
                }
93
94
                // throw an exception if the value is NOT in the array
95
                throw new \InvalidArgumentException(
96
                    sprintf('Found invalid attribute group name "%s"', $attributeGroupName)
97
                );
98
            }
99
        }
100
    }
101
}
102