Completed
Push — 19.x ( c94702...e76808 )
by Tim
09:06
created

LinkValidatorCallback   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 28 4
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Callbacks\LinkValidatorCallback
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\Product\Callbacks;
22
23
use TechDivision\Import\Callbacks\ArrayValidatorCallback;
24
25
/**
26
 * A callback implementation that validates the SKUs on the bound column.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-product
32
 * @link      http://www.techdivision.com
33
 */
34
class LinkValidatorCallback extends ArrayValidatorCallback
35
{
36
37
    /**
38
     * Will be invoked by the observer it has been registered for.
39
     *
40
     * @param string|null $attributeCode  The code of the attribute that has to be validated
41
     * @param string|null $attributeValue The attribute value to be validated
42
     *
43
     * @return mixed The modified value
44
     * @throws \InvalidArgumentException Is thrown, if the SKU is not already in the database
45
     */
46
    public function handle($attributeCode = null, $attributeValue = null)
47
    {
48
49
        // explode the SKUs from the link column
50
        if ($this->isNullable($skus = $this->getSubject()->explode($attributeValue))) {
51
            return;
52
        }
53
54
        // the validations for the attribute with the given code
55
        $validations = $this->getValidations();
56
57
        // iterate over the exploded SKUs and validate them
58
        foreach ($skus as $sku) {
59
            // if the passed SKU is in the array, return immediately
60
            if (in_array($sku, $validations)) {
61
                continue;
62
            }
63
64
            // throw an exception if the value is NOT in the array
65
            throw new \InvalidArgumentException(
66
                sprintf(
67
                    'Found not exisisting SKU "%s" in column "%s"',
68
                    $sku,
69
                    $attributeCode
70
                )
71
            );
72
        }
73
    }
74
}
75