AttributeSetNameValidatorCallback::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 22
ccs 0
cts 9
cp 0
crap 12
rs 10
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Callbacks\AttributeSetNameValidatorCallback
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2019 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-attribute-set
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Attribute\Set\Callbacks;
16
17
use TechDivision\Import\Attribute\Set\Utils\ColumnKeys;
18
use TechDivision\Import\Callbacks\IndexedArrayValidatorCallback;
19
20
/**
21
 * A callback implementation that validates the attribute group names in an attribute import.
22
 *
23
 * @author    Tim Wagner <[email protected]>
24
 * @copyright 2019 TechDivision GmbH <[email protected]>
25
 * @license   https://opensource.org/licenses/MIT
26
 * @link      https://github.com/techdivision/import-attribute-set
27
 * @link      http://www.techdivision.com
28
 */
29
class AttributeSetNameValidatorCallback extends IndexedArrayValidatorCallback
30
{
31
32
    /**
33
     * Will be invoked by the observer it has been registered for.
34
     *
35
     * @param string|null $attributeCode  The code of the attribute that has to be validated
36
     * @param string|null $attributeValue The attribute value to be validated
37
     *
38
     * @return mixed The modified value
39
     */
40
    public function handle($attributeCode = null, $attributeValue = null)
41
    {
42
43
        // load the subject instance
44
        $subject = $this->getSubject();
45
46
        // query whether or not the attribute value is emtpy
47
        if ($this->isNullable($attributeValue)) {
48
            return;
49
        }
50
51
        // load the validations for the attribute set with the given name
52
        $validations = $this->getValidations($subject->getValue(ColumnKeys::ENTITY_TYPE_CODE));
53
54
        // query whether or not the value is valid
55
        if (in_array($attributeValue, $validations)) {
56
            return;
57
        }
58
59
        // throw an exception if the value is NOT in the array
60
        throw new \InvalidArgumentException(
61
            sprintf('Found invalid attribute set name "%s"', $attributeValue)
62
        );
63
    }
64
}
65