Completed
Push — master ( c0a376...71f1b8 )
by Tim
14s
created

CustomerAttributeObserver::deleteIntAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Observers\CustomerAttributeObserver
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 2018 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-customer
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Customer\Observers;
22
23
use TechDivision\Import\Customer\Utils\ColumnKeys;
24
use TechDivision\Import\Customer\Utils\MemberNames;
25
use TechDivision\Import\Observers\AbstractAttributeObserver;
26
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
27
28
/**
29
 * Observer that creates/updates the customer's attributes.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2018 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-customer
35
 * @link      http://www.techdivision.com
36
 */
37
class CustomerAttributeObserver extends AbstractAttributeObserver
38
{
39
40
    /**
41
     * The customer bunch processor instance.
42
     *
43
     * @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface
44
     */
45
    protected $customerBunchProcessor;
46
47
    /**
48
     * Initialize the observer with the passed customer bunch processor instance.
49
     *
50
     * @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The customer bunch processor instance
51
     */
52
    public function __construct(CustomerBunchProcessorInterface $customerBunchProcessor)
53
    {
54
        $this->customerBunchProcessor = $customerBunchProcessor;
55
    }
56
57
    /**
58
     * Intializes the existing attributes for the entity with the passed primary key.
59
     *
60
     * @param string  $pk      The primary key of the entity to load the attributes for
61
     * @param integer $storeId The ID of the store view to load the attributes for
62
     *
63
     * @return array The entity attributes
64
     */
65
    protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId)
0 ignored issues
show
Unused Code introduced by
The parameter $storeId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
    protected function getAttributesByPrimaryKeyAndStoreId($pk, /** @scrutinizer ignore-unused */ $storeId)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $this->attributes = $this->getCustomerBunchProcessor()->getCustomerAttributesByEntityId($pk);
0 ignored issues
show
Bug introduced by
$pk of type string is incompatible with the type integer expected by parameter $entityId of TechDivision\Import\Cust...rAttributesByEntityId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $this->attributes = $this->getCustomerBunchProcessor()->getCustomerAttributesByEntityId(/** @scrutinizer ignore-type */ $pk);
Loading history...
68
    }
69
70
    /**
71
     * Return's the customer bunch processor instance.
72
     *
73
     * @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The customer bunch processor instance
74
     */
75
    protected function getCustomerBunchProcessor()
76
    {
77
        return $this->customerBunchProcessor;
78
    }
79
80
    /**
81
     * Returns the value(s) of the primary key column(s). As the primary key column can
82
     * also consist of two columns, the return value can be an array also.
83
     *
84
     * @return mixed The primary key value(s)
85
     */
86
    protected function getPrimaryKeyValue()
87
    {
88
89
        // initialize the array for the PK values
90
        $pk = array();
91
92
        // prepare the array with the PK values
93
        foreach ($this->getPrimaryKeyColumnName() as $columName) {
0 ignored issues
show
Bug introduced by
The expression $this->getPrimaryKeyColumnName() of type string is not traversable.
Loading history...
94
            $pk[] = $this->getValue($columName);
95
        }
96
97
        // return the array with the PK values
98
        return $pk;
99
    }
100
101
    /**
102
     * Prepare the attributes of the entity that has to be persisted.
103
     *
104
     * @return array|null The prepared attributes
105
     */
106
    protected function prepareAttributes()
107
    {
108
109
        // laod the callbacks for the actual attribute code
110
        $callbacks = $this->getCallbacksByType($this->attributeCode);
111
112
        // invoke the pre-cast callbacks
113
        foreach ($callbacks as $callback) {
114
            $this->attributeValue = $callback->handle($this);
115
        }
116
117
        // load the ID of the product that has been created recently
118
        $lastEntityId = $this->getPrimaryKey();
119
120
        // cast the value based on the backend type
121
        $castedValue = $this->castValueByBackendType($this->backendType, $this->attributeValue);
122
123
        // prepare the attribute values
124
        return $this->initializeEntity(
125
            array(
126
                $this->getPrimaryKeyMemberName() => $lastEntityId,
127
                MemberNames::ATTRIBUTE_ID       => $this->attributeId,
128
                MemberNames::VALUE              => $castedValue
129
            )
130
        );
131
    }
132
133
    /**
134
     * Return's the PK to create the customer => attribute relation.
135
     *
136
     * @return integer The PK to create the relation with
137
     */
138
    protected function getPrimaryKey()
139
    {
140
        return $this->getSubject()->getLastEntityId();
141
    }
142
143
    /**
144
     * Return's the PK column name to create the customer => attribute relation.
145
     *
146
     * @return string The PK column name
147
     */
148
    protected function getPrimaryKeyMemberName()
149
    {
150
        return MemberNames::ENTITY_ID;
151
    }
152
153
    /**
154
     * Return's the column name that contains the primary key.
155
     *
156
     * @return string the column name that contains the primary key
157
     */
158
    protected function getPrimaryKeyColumnName()
159
    {
160
        return array(ColumnKeys::EMAIL, ColumnKeys::WEBSITE);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(TechDivisio...ls\ColumnKeys::WEBSITE) returns the type array<integer,string> which is incompatible with the documented return type string.
Loading history...
161
    }
162
163
    /**
164
     * Queries whether or not the passed PK and store view code has already been processed.
165
     *
166
     * @param string $pk            The PK to check been processed
167
     * @param string $storeViewCode The store view code to check been processed
168
     *
169
     * @return boolean TRUE if the PK and store view code has been processed, else FALSE
170
     */
171
    protected function storeViewHasBeenProcessed($pk, $storeViewCode)
172
    {
173
        return $this->getSubject()->storeViewHasBeenProcessed($pk, $storeViewCode);
174
    }
175
176
    /**
177
     * Persist's the passed varchar attribute.
178
     *
179
     * @param array $attribute The attribute to persist
180
     *
181
     * @return void
182
     */
183
    protected function persistVarcharAttribute($attribute)
184
    {
185
        $this->getCustomerBunchProcessor()->persistCustomerVarcharAttribute($attribute);
186
    }
187
188
    /**
189
     * Persist's the passed integer attribute.
190
     *
191
     * @param array $attribute The attribute to persist
192
     *
193
     * @return void
194
     */
195
    protected function persistIntAttribute($attribute)
196
    {
197
        $this->getCustomerBunchProcessor()->persistCustomerIntAttribute($attribute);
198
    }
199
200
    /**
201
     * Persist's the passed decimal attribute.
202
     *
203
     * @param array $attribute The attribute to persist
204
     *
205
     * @return void
206
     */
207
    protected function persistDecimalAttribute($attribute)
208
    {
209
        $this->getCustomerBunchProcessor()->persistCustomerDecimalAttribute($attribute);
210
    }
211
212
    /**
213
     * Persist's the passed datetime attribute.
214
     *
215
     * @param array $attribute The attribute to persist
216
     *
217
     * @return void
218
     */
219
    protected function persistDatetimeAttribute($attribute)
220
    {
221
        $this->getCustomerBunchProcessor()->persistCustomerDatetimeAttribute($attribute);
222
    }
223
224
    /**
225
     * Persist's the passed text attribute.
226
     *
227
     * @param array $attribute The attribute to persist
228
     *
229
     * @return void
230
     */
231
    protected function persistTextAttribute($attribute)
232
    {
233
        $this->getCustomerBunchProcessor()->persistCustomerTextAttribute($attribute);
234
    }
235
236
    /**
237
     * Delete's the datetime attribute with the passed value ID.
238
     *
239
     * @param array       $row  The attributes of the entity to delete
240
     * @param string|null $name The name of the prepared statement that has to be executed
241
     *
242
     * @return void
243
     */
244
    protected function deleteDatetimeAttribute(array $row, $name = null)
245
    {
246
        $this->getCustomerBunchProcessor()->deleteCustomerDatetimeAttribute($row, $name);
247
    }
248
249
    /**
250
     * Delete's the decimal attribute with the passed value ID.
251
     *
252
     * @param array       $row  The attributes of the entity to delete
253
     * @param string|null $name The name of the prepared statement that has to be executed
254
     *
255
     * @return void
256
     */
257
    protected function deleteDecimalAttribute(array $row, $name = null)
258
    {
259
        $this->getCustomerBunchProcessor()->deleteCustomerDecimalAttribute($row, $name);
260
    }
261
262
    /**
263
     * Delete's the integer attribute with the passed value ID.
264
     *
265
     * @param array       $row  The attributes of the entity to delete
266
     * @param string|null $name The name of the prepared statement that has to be executed
267
     *
268
     * @return void
269
     */
270
    protected function deleteIntAttribute(array $row, $name = null)
271
    {
272
        $this->getCustomerBunchProcessor()->deleteCustomerIntAttribute($row, $name);
273
    }
274
275
    /**
276
     * Delete's the text attribute with the passed value ID.
277
     *
278
     * @param array       $row  The attributes of the entity to delete
279
     * @param string|null $name The name of the prepared statement that has to be executed
280
     *
281
     * @return void
282
     */
283
    protected function deleteTextAttribute(array $row, $name = null)
284
    {
285
        $this->getCustomerBunchProcessor()->deleteCustomerTextAttribute($row, $name);
286
    }
287
288
    /**
289
     * Delete's the varchar attribute with the passed value ID.
290
     *
291
     * @param array       $row  The attributes of the entity to delete
292
     * @param string|null $name The name of the prepared statement that has to be executed
293
     *
294
     * @return void
295
     */
296
    protected function deleteVarcharAttribute(array $row, $name = null)
297
    {
298
        return $this->getCustomerBunchProcessor()->deleteCustomerVarcharAttribute($row, $name);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getCustomerBunchP...rAttribute($row, $name) targeting TechDivision\Import\Cust...tomerVarcharAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
299
    }
300
}
301