Completed
Push — 13.x ( 892966...ecd694 )
by Tim
02:49
created

CustomerObserver::getStoreWebsiteIdByCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Customer\Observers\CustomerObserver
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\GenderKeys;
24
use TechDivision\Import\Customer\Utils\ColumnKeys;
25
use TechDivision\Import\Customer\Utils\MemberNames;
26
use TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface;
27
28
/**
29
 * Observer that create's the customer itself.
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 CustomerObserver extends AbstractCustomerImportObserver
38
{
39
40
    /**
41
     * The customer bunch processor instance.
42
     *
43
     * @var \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface
44
     */
45
    protected $customerBunchProcessor;
46
47
    /**
48
     * The array with the available gender keys.
49
     *
50
     * @var array
51
     */
52
    protected $availableGenders = array(
53
        'Male'          => GenderKeys::GENDER_MALE,
54
        'Female'        => GenderKeys::GENDER_FEMALE,
55
        'Not Specified' => GenderKeys::GENDER_NOT_SPECIFIED
56
    );
57
58
    /**
59
     * Initialize the observer with the passed customer bunch processor instance.
60
     *
61
     * @param \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface $customerBunchProcessor The customer bunch processor instance
62
     */
63
    public function __construct(CustomerBunchProcessorInterface $customerBunchProcessor)
64
    {
65
        $this->customerBunchProcessor = $customerBunchProcessor;
66
    }
67
68
    /**
69
     * Return's the customer bunch processor instance.
70
     *
71
     * @return \TechDivision\Import\Customer\Services\CustomerBunchProcessorInterface The customer bunch processor instance
72
     */
73
    protected function getCustomerBunchProcessor()
74
    {
75
        return $this->customerBunchProcessor;
76
    }
77
78
    /**
79
     * Process the observer's business logic.
80
     *
81
     * @return void
82
     */
83
    protected function process()
84
    {
85
86
        // load email and website code
87
        $email = $this->getValue(ColumnKeys::EMAIL);
88
        $website = $this->getValue(ColumnKeys::WEBSITE);
89
90
        // query whether or not, we've found a new SKU => means we've found a new customer
91
        if ($this->hasBeenProcessed(array($email, $website))) {
92
            return;
93
        }
94
95
        // prepare the static entity values
96
        $customer = $this->initializeCustomer($this->prepareAttributes());
97
98
        // insert the entity and set the entity ID
99
        $this->setLastEntityId($this->persistCustomer($customer));
100
    }
101
102
    /**
103
     * Prepare the attributes of the entity that has to be persisted.
104
     *
105
     * @return array The prepared attributes
106
     */
107
    protected function prepareAttributes()
108
    {
109
110
        // initialize the customer values
111
        $email = $this->getValue(ColumnKeys::EMAIL);
112
        $groupId = $this->getValue(ColumnKeys::GROUP_ID);
113
        $storeId = $this->getValue(ColumnKeys::STORE_ID);
114
        $disableAutoGroupChange = $this->getValue(ColumnKeys::DISABLE_AUTO_GROUP_CHANGE);
115
        $prefix = $this->getValue(ColumnKeys::PREFIX);
116
        $firstname = $this->getValue(ColumnKeys::FIRSTNAME);
117
        $middlename = $this->getValue(ColumnKeys::MIDDLENAME);
118
        $lastname = $this->getValue(ColumnKeys::LASTNAME);
119
        $suffix = $this->getValue(ColumnKeys::SUFFIX);
120
        $passwordHash = $this->getValue(ColumnKeys::PASSWORD_HASH);
121
        $rpToken = $this->getValue(ColumnKeys::RP_TOKEN);
122
        $defaultShipping = $this->getValue(ColumnKeys::ADDRESS_DEFAULT_SHIPPING);
123
        $defaultBilling = $this->getValue(ColumnKeys::ADDRESS_DEFAULT_BILLING);
124
        $taxvat = $this->getValue(ColumnKeys::TAXVAT);
125
        $confirmation = $this->getValue(ColumnKeys::CONFIRMATION);
126
        $gender = $this->getGenderByValue($this->getValue(ColumnKeys::GENDER));
127
128
        // load the customer's addtional attributes
129
        $createdIn = $this->getValue(ColumnKeys::CREATED_IN);
130
        $incrementId = null;
131
        $isActive = 1;
132
        $failuresNum = 0;
133
        $firstFailure = null;
134
        $lockExpires = null;
135
136
        // prepare the date format for the created at/updated at dates
137
        $websiteId = $this->getStoreWebsiteIdByCode($this->getValue(ColumnKeys::WEBSITE));
138
        $dob = $this->getValue(ColumnKeys::DOB, date('Y-m-d H:i:s'), array($this, 'formatDate'));
139
        $createdAt = $this->getValue(ColumnKeys::CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
140
        $updatedAt = $this->getValue(ColumnKeys::UPDATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
141
        $rpTokenCreatedAt = $this->getValue(ColumnKeys::RP_TOKEN_CREATED_AT, date('Y-m-d H:i:s'), array($this, 'formatDate'));
142
143
        // return the prepared customer
144
        return $this->initializeEntity(
145
            array(
146
                MemberNames::WEBSITE_ID                => $websiteId,
147
                MemberNames::EMAIL                     => $email,
148
                MemberNames::GROUP_ID                  => $groupId,
149
                MemberNames::INCREMENT_ID              => $incrementId,
150
                MemberNames::STORE_ID                  => $storeId,
151
                MemberNames::CREATED_AT                => $createdAt,
152
                MemberNames::UPDATED_AT                => $updatedAt,
153
                MemberNames::IS_ACTIVE                 => $isActive,
154
                MemberNames::DISABLE_AUTO_GROUP_CHANGE => $disableAutoGroupChange,
155
                MemberNames::CREATED_IN                => $createdIn,
156
                MemberNames::PREFIX                    => $prefix,
157
                MemberNames::FIRSTNAME                 => $firstname,
158
                MemberNames::MIDDLENAME                => $middlename,
159
                MemberNames::LASTNAME                  => $lastname,
160
                MemberNames::SUFFIX                    => $suffix,
161
                MemberNames::DOB                       => $dob,
162
                MemberNames::PASSWORD_HASH             => $passwordHash,
163
                MemberNames::RP_TOKEN                  => $rpToken,
164
                MemberNames::RP_TOKEN_CREATED_AT       => $rpTokenCreatedAt,
165
                MemberNames::DEFAULT_BILLING           => $defaultBilling,
166
                MemberNames::DEFAULT_SHIPPING          => $defaultShipping,
167
                MemberNames::TAXVAT                    => $taxvat,
168
                MemberNames::CONFIRMATION              => $confirmation,
169
                MemberNames::GENDER                    => $gender,
170
                MemberNames::FAILURES_NUM              => $failuresNum,
171
                MemberNames::FIRST_FAILURE             => $firstFailure,
172
                MemberNames::LOCK_EXPIRES              => $lockExpires
173
            )
174
        );
175
    }
176
177
    /**
178
     * Initialize the customer with the passed attributes and returns an instance.
179
     *
180
     * @param array $attr The customer attributes
181
     *
182
     * @return array The initialized customer
183
     */
184
    protected function initializeCustomer(array $attr)
185
    {
186
187
        // load the customer with the passed SKU and merge it with the attributes
188
        if ($entity = $this->loadCustomerByEmailAndWebsiteId($attr[MemberNames::EMAIL], $attr[MemberNames::WEBSITE_ID])) {
189
            return $this->mergeEntity($entity, $attr);
190
        }
191
192
        // otherwise simply return the attributes
193
        return $attr;
194
    }
195
196
    /**
197
     * Return's the gender ID for the passed value.
198
     *
199
     * @param string $value The value to return the gender ID for
200
     *
201
     * @return integer The gender ID
202
     * @throws \Exception Is thrown, if the gender ID with the requested value is not available
203
     */
204
    protected function getGenderByValue($value)
205
    {
206
207
        // query whether or not, the requested gender ID is available
208
        if (isset($this->availableGenders[$value])) {
209
            return (integer) $this->availableGenders[$value];
210
        }
211
212
        // throw an exception, if not
213
        throw new \Exception(
214
            $this->appendExceptionSuffix(
215
                sprintf('Found invalid gender %s', $value)
216
            )
217
        );
218
    }
219
220
    /**
221
     * Return's the store website for the passed code.
222
     *
223
     * @param string $code The code of the store website to return the ID for
224
     *
225
     * @return integer The store website ID
226
     */
227
    protected function getStoreWebsiteIdByCode($code)
228
    {
229
        return $this->getSubject()->getStoreWebsiteIdByCode($code);
230
    }
231
232
    /**
233
     * Return's the customer with the passed email and website ID.
234
     *
235
     * @param string $email     The email of the customer to return
236
     * @param string $websiteId The website ID of the customer to return
237
     *
238
     * @return array|null The customer
239
     */
240
    protected function loadCustomerByEmailAndWebsiteId($email, $websiteId)
241
    {
242
        return $this->getCustomerBunchProcessor()->loadCustomerByEmailAndWebsiteId($email, $websiteId);
243
    }
244
245
    /**
246
     * Persist's the passed customer data and return's the ID.
247
     *
248
     * @param array $customer The customer data to persist
249
     *
250
     * @return string The ID of the persisted entity
251
     */
252
    protected function persistCustomer($customer)
253
    {
254
        return $this->getCustomerBunchProcessor()->persistCustomer($customer);
255
    }
256
257
    /**
258
     * Return's the attribute set of the product that has to be created.
259
     *
260
     * @return array The attribute set
261
     */
262
    protected function getAttributeSet()
263
    {
264
        return $this->getSubject()->getAttributeSet();
265
    }
266
267
    /**
268
     * Set's the ID of the customer that has been created recently.
269
     *
270
     * @param string $lastEntityId The entity ID
271
     *
272
     * @return void
273
     */
274
    protected function setLastEntityId($lastEntityId)
275
    {
276
        $this->getSubject()->setLastEntityId($lastEntityId);
277
    }
278
}
279