Completed
Push — master ( aee93b...d79f49 )
by Tim
02:06
created

GenericValidationObserver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 145
ccs 0
cts 58
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createObserver() 0 9 1
A handle() 0 13 1
B process() 0 40 5
A reverseMapHeaderNameToColumnName() 0 4 1
A getEmptyAttributeValueConstant() 0 4 1
A getCallbacksByType() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\GenericValidationObserver
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 2021 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\Utils\RegistryKeys;
24
use TechDivision\Import\Subjects\SubjectInterface;
25
use TechDivision\Import\Services\RegistryProcessorInterface;
26
27
/**
28
 * Observer that invokes the callbacks to validate the actual row.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2021 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 GenericValidationObserver extends AbstractObserver implements ObserverFactoryInterface
37
{
38
39
    /**
40
     * The registry processor instance.
41
     *
42
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
43
     */
44
    protected $registryProcessor;
45
46
    /**
47
     * Array with virtual column name mappings (this is a temporary
48
     * solution till techdivision/import#179 as been implemented).
49
     *
50
     * @var array
51
     */
52
    protected $reverseHeaderMappings = array();
53
54
    /**
55
     * Initializes the observer with the registry processor instance.
56
     *
57
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance
58
     */
59
    public function __construct(RegistryProcessorInterface $registryProcessor)
60
    {
61
        $this->registryProcessor = $registryProcessor;
62
    }
63
64
    /**
65
     * Will be invoked by the observer visitor when a factory has been defined to create the observer instance.
66
     *
67
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
68
     *
69
     * @return \TechDivision\Import\Observers\ObserverInterface The observer instance
70
     */
71
    public function createObserver(SubjectInterface $subject)
72
    {
73
74
        // initialize the array for the reverse header mappings
75
        $this->reverseHeaderMappings = array_flip($subject->getHeaderMappings());
76
77
        // return the intialized instance
78
        return $this;
79
    }
80
81
    /**
82
     * Will be invoked by the action on the events the listener has been registered for.
83
     *
84
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
85
     *
86
     * @return array The modified row
87
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
88
     */
89
    public function handle(SubjectInterface $subject)
90
    {
91
92
        // initialize the row
93
        $this->setSubject($subject);
94
        $this->setRow($subject->getRow());
95
96
        // process the functionality and return the row
97
        $this->process();
98
99
        // return the processed row
100
        return $this->getRow();
101
    }
102
103
    /**
104
     * Process the observer's business logic.
105
     *
106
     * @return array The processed row
107
     */
108
    protected function process()
109
    {
110
111
        // load the available header names
112
        $headerNames = array_keys($this->getHeaders());
113
        $emptyValueDefinition = $this->getEmptyAttributeValueConstant();
114
115
        // iterate over the custom validations
116
        foreach ($headerNames as $headerName) {
117
            // load the attribute value from the row
118
            $attributeValue = $this->getValue($headerName);
119
            // reverse map the header name to the original column name
120
            $columnName = $this->reverseMapHeaderNameToColumnName($headerName);
121
            // load the callbacks for the actual attribute code
122
            $callbacks = $this->getCallbacksByType($columnName);
123
            // invoke the registered callbacks
124
            foreach ($callbacks as $callback) {
125
                try {
126
                    // query whether or not to cleanup complete attribute
127
                    if ($attributeValue === $emptyValueDefinition) {
128
                        continue;
129
                    }
130
                    $callback->handle($columnName, $attributeValue);
131
                } catch (\InvalidArgumentException $iea) {
132
                    // add the the validation result to the status
133
                    $this->mergeStatus(
134
                        array(
135
                            RegistryKeys::VALIDATIONS => array(
136
                                basename($this->getFilename()) => array(
137
                                    $this->getSubject()->getLineNumber() => array(
138
                                        $columnName => $iea->getMessage()
139
                                    )
140
                                )
141
                            )
142
                        )
143
                    );
144
                }
145
            }
146
        }
147
    }
148
149
    /**
150
     * Reverse map the passed header name, to the original column name.
151
     *
152
     * @param string $headerName The header name to reverse map
153
     *
154
     * @return string The original column name
155
     */
156
    protected function reverseMapHeaderNameToColumnName(string $headerName) : string
157
    {
158
        return $this->reverseHeaderMappings[$headerName] ?? $headerName;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    protected function getEmptyAttributeValueConstant() : string
165
    {
166
        return $this->getSubject()->getConfiguration()->getConfiguration()->getEmptyAttributeValueConstant();
167
    }
168
169
    /**
170
     * Return's the array with callbacks for the passed type.
171
     *
172
     * @param string $type The type of the callbacks to return
173
     *
174
     * @return array The callbacks
175
     */
176
    protected function getCallbacksByType($type)
177
    {
178
        return $this->getSubject()->getCallbacksByType($type);
179
    }
180
}
181