Completed
Push — master ( 208d4f...cc81bc )
by Tim
10s
created

AbstractCustomerSubject::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Subjects\AbstractCustomerSubject
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\Subjects;
22
23
use TechDivision\Import\Customer\Utils\MemberNames;
24
use TechDivision\Import\Subjects\AbstractEavSubject;
25
use TechDivision\Import\Subjects\EntitySubjectInterface;
26
use TechDivision\Import\Customer\Utils\ConfigurationKeys;
27
use TechDivision\Import\Customer\Utils\RegistryKeys;
28
29
/**
30
 * The abstract customer subject implementation that provides basic customer
31
 * handling business logic.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2018 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-customer
37
 * @link      http://www.techdivision.com
38
 */
39
abstract class AbstractCustomerSubject extends AbstractEavSubject implements EntitySubjectInterface
40
{
41
42
    /**
43
     * The ID of the product that has been created recently.
44
     *
45
     * @var string
46
     */
47
    protected $lastEntityId;
48
49
    /**
50
     * The identifier (email + website) of the customer that has been created recently.
51
     *
52
     * @var string
53
     */
54
    protected $lastIdentifier;
55
56
    /**
57
     * The Magento 2 configuration.
58
     *
59
     * @var array
60
     */
61
    protected $coreConfigData;
62
63
    /**
64
     * The mapping for the mail webseite code to the created entity IDs.
65
     *
66
     * @var array
67
     */
68
    protected $customerIdentifierEntityIdMapping = array();
69
70
    /**
71
     * The available store websites.
72
     *
73
     * @var array
74
     */
75
    protected $storeWebsites = array();
76
77
    /**
78
     * Sets the customer identifier of the last imported customer.
79
     *
80
     * @param array $identifier The unique customer identifier
81
     *
82
     * @return void
83
     */
84
    public function setLastIdentifier(array $identifier)
85
    {
86
        $this->lastIdentifier = $identifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $identifier of type array is incompatible with the declared type string of property $lastIdentifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
87
    }
88
89
    /**
90
     * Return's the SKU of the last imported product.
91
     *
92
     * @return string|null The SKU
93
     */
94
    public function getLastIdentifier()
95
    {
96
        return $this->lastIdentifier;
97
    }
98
99
    /**
100
     * Set's the ID of the product that has been created recently.
101
     *
102
     * @param string $lastEntityId The entity ID
103
     *
104
     * @return void
105
     */
106
    public function setLastEntityId($lastEntityId)
107
    {
108
        $this->lastEntityId = $lastEntityId;
109
    }
110
111
    /**
112
     * Return's the ID of the product that has been created recently.
113
     *
114
     * @return string The entity Id
115
     */
116
    public function getLastEntityId()
117
    {
118
        return $this->lastEntityId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->lastEntityId returns the type string which is incompatible with the return type mandated by TechDivision\Import\Subj...face::getLastEntityId() of integer.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
119
    }
120
121
    /**
122
     * Queries whether or not the customer with the passed identifier has already been processed.
123
     *
124
     * @param array $identifier The customer identifier to check
125
     *
126
     * @return boolean TRUE if the customer has been processed, else FALSE
127
     */
128
    public function hasBeenProcessed(array $identifier)
129
    {
130
131
        // explode the identifier (we need email + website code)
132
        list ($email, $website) = $identifier;
133
134
        // query whether or not the entity ID has already been mapped
135
        return isset($this->customerIdentifierEntityIdMapping[$email][$website]);
136
    }
137
138
    /**
139
     * Queries whether or not the passed PK and store view code has already been processed.
140
     *
141
     * @param string $pk            The PK to check been processed
142
     * @param string $storeViewCode The store view code to check been processed
143
     *
144
     * @return boolean TRUE if the PK and store view code has been processed, else FALSE
145
     */
146
    public function storeViewHasBeenProcessed(array $identifier, $storeViewCode)
147
    {
148
149
        // explode the identifier (we need email + website code)
150
        list ($email, $website) = $identifier;
151
152
        // query whether or not the store view code has already been mapped to the customer identifier
153
        return isset($this->customerIdentifierEntityIdMapping[$email][$website]) && $this->customerIdentifierEntityIdMapping[$email][$website] === $storeViewCode;
154
    }
155
156
    /**
157
     * Add the passed mail address/website code => entity ID mapping.
158
     *
159
     * @param string $email       The mail address of the customer
160
     * @param string $websiteCode The website code the customer is bound to
161
     *
162
     * @return void
163
     */
164
    public function addCustomerIdentifierEntityIdMapping($email, $website)
165
    {
166
        $this->customerIdentifierEntityIdMapping[$email][$website] = $this->getLastEntityId();
167
    }
168
169
    /**
170
     * Intializes the previously loaded global data for exactly one bunch.
171
     *
172
     * @param string $serial The serial of the actual import
173
     *
174
     * @return void
175
     */
176
    public function setUp($serial)
177
    {
178
179
        // load the status of the actual import
180
        $status = $this->getRegistryProcessor()->getAttribute($serial);
181
182
        // load the global data we've prepared initially
183
        $this->storeWebsites =  $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES];
184
185
        // invoke the parent method
186
        parent::setUp($serial);
187
    }
188
189
    /**
190
     * Clean up the global data after importing the bunch.
191
     *
192
     * @param string $serial The serial of the actual import
193
     *
194
     * @return void
195
     */
196
    public function tearDown($serial)
197
    {
198
199
        // load the registry processor
200
        $registryProcessor = $this->getRegistryProcessor();
201
202
        // update the status
203
        $registryProcessor->mergeAttributesRecursive(
204
            $serial,
205
            array(
206
                RegistryKeys::CUSTOMER_IDENTIFIER_ENTITY_ID_MAPPING => $this->customerIdentifierEntityIdMapping,
207
            )
208
        );
209
210
        // invoke the parent method
211
        parent::tearDown($serial);
212
    }
213
214
    /**
215
     * Return's the store ID of the actual row, or of the default store
216
     * if no store view code is set in the CSV file.
217
     *
218
     * @param string|null $default The default store view code to use, if no store view code is set in the CSV file
219
     *
220
     * @return integer The ID of the actual store
221
     * @throws \Exception Is thrown, if the store with the actual code is not available
222
     */
223
    public function getRowStoreId($default = null)
224
    {
225
226
        // initialize the default store view code, if not passed
227
        if ($default == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $default of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
228
            $defaultStore = $this->getDefaultStore();
229
            $default = $defaultStore[MemberNames::CODE];
230
        }
231
232
        // load the store view code to create the product/attributes for
233
        $storeViewCode = $this->getStoreViewCode($default);
234
235
        // query whether or not, the requested store is available
236
        if (isset($this->stores[$storeViewCode])) {
237
            return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID];
238
        }
239
240
        // throw an exception, if not
241
        throw new \Exception(
242
            $this->appendExceptionSuffix(
243
                sprintf('Found invalid store view code %s', $storeViewCode)
244
            )
245
        );
246
    }
247
248
    /**
249
     * Merge the columns from the configuration with all image type columns to define which
250
     * columns should be cleaned-up.
251
     *
252
     * @return array The columns that has to be cleaned-up
253
     */
254
    public function getCleanUpColumns()
255
    {
256
        return $this->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getConfigu...CLEAN_UP_EMPTY_COLUMNS) returns the type string which is incompatible with the documented return type array.
Loading history...
257
    }
258
259
    /**
260
     * Return's the store website for the passed code.
261
     *
262
     * @param string $code The code of the store website to return the ID for
263
     *
264
     * @return integer The store website ID
265
     * @throws \Exception Is thrown, if the store website with the requested code is not available
266
     */
267
    public function getStoreWebsiteIdByCode($code)
268
    {
269
270
        // query whether or not, the requested store website is available
271
        if (isset($this->storeWebsites[$code])) {
272
            return (integer) $this->storeWebsites[$code][MemberNames::WEBSITE_ID];
273
        }
274
275
        // throw an exception, if not
276
        throw new \Exception(
277
            $this->appendExceptionSuffix(
278
                sprintf('Found invalid website code %s', $code)
279
            )
280
        );
281
    }
282
}
283