Completed
Push — pac-57--delete-eav-relation ( 96596e )
by
unknown
10:02
created

getEmptyAttributeValueConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\GenericValidatorObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Observers;
22
23
use TechDivision\Import\Subjects\SubjectInterface;
24
use TechDivision\Import\Services\RegistryProcessorInterface;
25
use TechDivision\Import\Utils\RegistryKeys;
26
27
/**
28
 * Observer that invokes the callbacks to validate the actual row.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 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
34
 * @link      http://www.techdivision.com
35
 */
36
class GenericValidatorObserver extends AbstractObserver
37
{
38
39
    /**
40
     * The registry processor instance.
41
     *
42
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
43
     */
44
    protected $registryProcessor;
45
46
    /**
47
     * Initializes the callback with the loader instance.
48
     *
49
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance
50
     */
51
    public function __construct(RegistryProcessorInterface $registryProcessor)
52
    {
53
        $this->registryProcessor = $registryProcessor;
54
    }
55
56
    /**
57
     * Will be invoked by the action on the events the listener has been registered for.
58
     *
59
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
60
     *
61
     * @return array The modified row
62
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
63
     */
64
    public function handle(SubjectInterface $subject)
65
    {
66
67
        // initialize the row
68
        $this->setSubject($subject);
69
        $this->setRow($subject->getRow());
70
71
        // process the functionality and return the row
72
        $this->process();
73
74
        // return the processed row
75
        return $this->getRow();
76
    }
77
78
    /**
79
     * Process the observer's business logic.
80
     *
81
     * @return void
82
     */
83
    protected function process()
84
    {
85
86
        // load the available header names
87
        $headerNames = array_keys($this->getHeaders());
88
        $emptyValueDefinition = $this->getEmptyAttributeValueConstant();
89
        // iterate over the custom validations
90
        foreach ($headerNames as $headerName) {
91
            // map the header name to the attribute code, if an mapping is available
92
            $attributeCode = $this->mapAttributeCodeByHeaderMapping($headerName);
93
            // load the attribute value from the row
94
            $attributeValue = $this->getValue($attributeCode);
95
            // load the callbacks for the actual attribute code
96
            $callbacks = $this->getCallbacksByType($attributeCode);
97
            // invoke the registered callbacks
98
            foreach ($callbacks as $callback) {
99
                try {
100
                    // query whether or not to cleanup complete attribute
101
                    if ($attributeValue === $emptyValueDefinition) {
102
                        continue;
103
                    }
104
                    $callback->handle($attributeCode, $attributeValue);
105
                } catch (\InvalidArgumentException $iea) {
106
                    // add the the validation result to the status
107
                    $this->mergeStatus(
108
                        array(
109
                            RegistryKeys::VALIDATIONS => array(
110
                                basename($this->getFilename()) => array(
111
                                    $this->getSubject()->getLineNumber() => array(
112
                                        $attributeCode => $iea->getMessage()
113
                                    )
114
                                )
115
                            )
116
                        )
117
                    );
118
                }
119
            }
120
        }
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getEmptyAttributeValueConstant()
127
    {
128
        return $this->getSubject()->getConfiguration()->getConfiguration()->getEmptyAttributeValueConstant();
129
    }
130
131
    /**
132
     * Map the passed attribute code, if a header mapping exists and return the
133
     * mapped mapping.
134
     *
135
     * @param string $attributeCode The attribute code to map
136
     *
137
     * @return string The mapped attribute code, or the original one
138
     */
139
    protected function mapAttributeCodeByHeaderMapping($attributeCode)
140
    {
141
        return $this->getSubject()->mapAttributeCodeByHeaderMapping($attributeCode);
142
    }
143
144
    /**
145
     * Return's the array with callbacks for the passed type.
146
     *
147
     * @param string $type The type of the callbacks to return
148
     *
149
     * @return array The callbacks
150
     */
151
    protected function getCallbacksByType($type)
152
    {
153
        return $this->getSubject()->getCallbacksByType($type);
154
    }
155
}
156