Completed
Pull Request — develop (#1)
by Carlo
04:21
created

SalesOrder::_validateRegistrationCustomer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
/**
3
 * SalesOrder
4
 */
5
6
namespace Magefix\Fixture\Builder;
7
8
use Mage;
9
use Mage_Checkout_Model_Type_Onepage;
10
use Mage_Customer_Model_Group;
11
use Magefix\Exceptions\UndefinedAttributes;
12
use Magefix\Exceptions\UndefinedQuoteAddresses;
13
use Magefix\Exceptions\UndefinedQuoteProducts;
14
use Magefix\Exceptions\UnknownQuoteAddressType;
15
16
/**
17
 * Class SalesOrder
18
 *
19
 * @package Magefix\Fixture\Builder
20
 * @author  Carlo Tasca <[email protected]>
21
 */
22
class SalesOrder extends AbstractBuilder
23
{
24
    /**
25
     * @var array
26
     */
27
    private $_quoteProducts = [];
28
29
    /**
30
     * @throws UndefinedAttributes
31
     * @throws UnknownQuoteAddressType
32
     *
33
     */
34
    public function build()
35
    {
36
        $defaultData = $this->_getMageModelData() ? $this->_getMageModelData() : [];
37
        $fixtureData = $this->_getFixtureAttributes();
38
        $mergedData  = array_merge($defaultData, $fixtureData);
39
40
        $this->_getMageModel()->setData($mergedData);
41
        $this->_buildQuoteProductFixtures();
42
        $this->_addProductsToQuote();
43
        $this->_addAddressesToQuote();
44
        $this->_setShippingMethod();
45
        $this->_setCheckoutMethod();
46
        $this->_setPaymentMethod();
47
48
        return $this->_saveFixture();
49
    }
50
51
    /**
52
     * @throws UndefinedQuoteProducts
53
     * @throws \Magefix\Fixture\Factory\UndefinedFixtureModel
54
     *
55
     */
56
    protected function _buildQuoteProductFixtures()
57
    {
58
        $this->_throwUndefinedQuoteProductsException();
59
        $this->_quoteProducts = $this->_buildManySimple('quote_products');
60
    }
61
62
63
    protected function _addProductsToQuote()
64
    {
65
        foreach ($this->_quoteProducts as $product) {
66
            $this->_getMageModel()->addProduct($product);
67
        }
68
    }
69
70
    /**
71
     * @throws UndefinedQuoteAddresses
72
     * @throws UnknownQuoteAddressType
73
     *
74
     */
75
    protected function _addAddressesToQuote()
76
    {
77
        $this->_throwUndefinedQuoteAddressesException();
78
79
        foreach ($this->_data['fixture']['addresses'] as $addressType => $address) {
80
            switch ($addressType) {
81
                case 'billing_and_shipping':
82
                    $this->_setQuoteAddress($addressType, true);
83
                    break;
84
                case ('billing' || 'shipping'):
85
                    $this->_setQuoteAddress($addressType, false);
86
                    break;
87
                default:
88
                    throw new UnknownQuoteAddressType(
89
                        'Sales Order Fixture: Unknown quote address type. Check fixture yml.'
90
                    );
91
            }
92
        }
93
    }
94
95
    /**
96
     * @param $addressType
97
     *
98
     * @param $sameAsBilling
99
     *
100
     * @return array
101
     */
102
    protected function _setQuoteAddress($addressType, $sameAsBilling)
103
    {
104
        $address = $this->_processFixtureAttributes($this->_data['fixture']['addresses'][$addressType]);
105
106
        if ($sameAsBilling === true) {
107
            $this->_getMageModel()->getBillingAddress()->addData($address);
108
            $this->_getMageModel()->getShippingAddress()->addData($address);
109
        }
110
111
        if ($addressType == 'billing' && $sameAsBilling === false) {
112
            $this->_getMageModel()->getBillingAddress()->addData($address);
113
        } elseif ($addressType == 'shipping' && $sameAsBilling === false) {
114
            $this->_getMageModel()->getShippingAddress()->addData($address);
115
        }
116
    }
117
118
    protected function _setShippingMethod()
119
    {
120
        $this->_validateShippingMethodData();
121
122
        $shippingData = $this->_processFixtureAttributes($this->_data['fixture']['shipping_method']);
123
        $this->_setShippingData($shippingData);
124
    }
125
126
    /**
127
     * @throws UndefinedAttributes
128
     *
129
     */
130
    protected function _validateShippingMethodData()
131
    {
132
        $this->_throwUndefinedAttributesException(
133
            isset($this->_data['fixture']['shipping_method']['carrier']),
134
            'Sales Order Fixture: Shipping carrier has not been defined. Check fixture yml.'
135
        );
136
137
        $this->_throwUndefinedAttributesException(
138
            isset($this->_data['fixture']['shipping_method']['method']),
139
            'Sales Order Fixture: Shipping method has not been defined. Check fixture yml.'
140
        );
141
142
        $this->_throwUndefinedAttributesException(
143
            isset($this->_data['fixture']['shipping_method']['free_shipping']),
144
            'Sales Order Fixture: Free shipping has not been defined. Check fixture yml.'
145
        );
146
147
        $this->_throwUndefinedAttributesException(
148
            isset($this->_data['fixture']['shipping_method']['collect_totals']),
149
            'Sales Order Fixture: Collect totals has not been defined. Check fixture yml.'
150
        );
151
152
        $this->_throwUndefinedAttributesException(
153
            isset($this->_data['fixture']['shipping_method']['collect_shipping_rates']),
154
            'Sales Order Fixture: Collect shipping rates has not been defined. Check fixture yml.'
155
        );
156
    }
157
158
    /**
159
     * @param $shippingData
160
     *
161
     */
162
    protected function _setShippingData($shippingData)
163
    {
164
        $shippingAddress = $this->_getMageModel()->getShippingAddress();
165
        $shippingAddress->setShippingMethod($shippingData['method']);
166
        $shippingAddress->setShippingDescription($shippingData['description']);
167
168
        if ($shippingData['collect_shipping_rates']) {
169
            $shippingAddress->setCollectShippingRates(true);
170
            $shippingAddress->collectShippingRates();
171
        }
172
173
        if ($shippingData['collect_totals']) {
174
            $this->_getMageModel()->collectTotals();
175
        }
176
177
        if ($shippingData['free_shipping']) {
178
            $this->_getMageModel()->setFreeShipping(true);
179
        }
180
    }
181
182
    /**
183
     *
184
     */
185
    protected function _setCheckoutMethod()
186
    {
187
        $this->_validateCheckoutMethod();
188
        $checkoutMethodData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']);
189
        $this->_getMageModel()->setCheckoutMethod($checkoutMethodData['method']);
190
        $this->_setCheckoutMethodGuest($checkoutMethodData);
191
192
        if ($this->_isRegisterCheckout($checkoutMethodData)) {
193
            $customerData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']['customer']);
194
            $this->_setCheckoutMethodRegister($customerData);
195
        }
196
    }
197
198
    /**
199
     * @param $method
200
     * @param $checkoutMethodData
201
     *
202
     * @return bool
203
     */
204
    protected function _isCheckoutMethod($method, $checkoutMethodData)
205
    {
206
        $isGuestCheckout = false;
207
208
        if ($checkoutMethodData['method'] == $method) {
209
            $isGuestCheckout = true;
210
        }
211
212
        return $isGuestCheckout;
213
    }
214
215
    /**
216
     * @param array $checkoutMethodData
217
     *
218
     * @return bool
219
     *
220
     */
221
    protected function _isGuestCheckout(array $checkoutMethodData)
222
    {
223
        return $this->_isCheckoutMethod(Mage_Checkout_Model_Type_Onepage::METHOD_GUEST, $checkoutMethodData);
224
    }
225
226
    /**
227
     * @param array $checkoutMethodData
228
     *
229
     * @return bool
230
     *
231
     */
232
    protected function _isRegisterCheckout(array $checkoutMethodData)
233
    {
234
        return $this->_isCheckoutMethod(Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER, $checkoutMethodData);
235
    }
236
237
    /**
238
     * @param array $checkoutMethodData
239
     *
240
     * @return bool
241
     *
242
     */
243
    protected function _isCustomerCheckout(array $checkoutMethodData)
244
    {
245
        return $this->_isCheckoutMethod(Mage_Checkout_Model_Type_Onepage::METHOD_CUSTOMER, $checkoutMethodData);
246
    }
247
248
249
    /**
250
     * @param array $checkoutMethodData
251
     *
252
     */
253
    protected function _setCheckoutMethodGuest(array $checkoutMethodData)
254
    {
255
        if ($this->_isGuestCheckout($checkoutMethodData)) {
256
            $this->_getMageModel()->setCustomerId(null)
257
                ->setCustomerEmail($this->_getMageModel()->getBillingAddress()->getEmail())
258
                ->setCustomerIsGuest(true)
259
                ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
260
        }
261
    }
262
263
    /**
264
     * @param array $customerRegisterData
265
     *
266
     */
267
    protected function _setCheckoutMethodRegister(array $customerRegisterData)
268
    {
269
        $dob = $this->_setCustomerRegistrationOptionalData($customerRegisterData);
270
271
        $customer = Mage::getModel($customerRegisterData['model']);
272
        $this->_getMageModel()->setPasswordHash($customer->encryptPassword($customerRegisterData['password']));
273
        $customer->setData($customerRegisterData);
274
275
        if (isset($customerRegisterData['dob'])) {
276
            $customer->setDob($dob);
277
        }
278
279
        $this->_validateRegistrationCustomer($customerRegisterData, $customer);
280
    }
281
282
    /**
283
     * @param array $customerRegisterData
284
     *
285
     * @return date|bool
286
     *
287
     */
288
    protected function _setCustomerRegistrationOptionalData(array $customerRegisterData)
289
    {
290
        $dob = false;
291
        if (isset($customerRegisterData['dob'])) {
292
            $dob = Mage::app()->getLocale()->date($customerRegisterData['dob'], null, null, false)->toString(
293
                'yyyy-MM-dd'
294
            );
295
296
            $this->_getMageModel()->setCustomerDob($dob);
297
        }
298
299
        if (isset($customerRegisterData['taxvat'])) {
300
            $this->_getMageModel()->setCustomerTaxvat($customerRegisterData['taxvat']);
301
        }
302
303
        if (isset($customerRegisterData['gender'])) {
304
            $this->_getMageModel()->setCustomerGender($customerRegisterData['gender']);
305
        }
306
307
        return $dob;
308
    }
309
310
    /**
311
     * @param array $customerRegisterData
312
     * @param       $customer
313
     *
314
     */
315
    protected function _validateRegistrationCustomer(array $customerRegisterData, $customer)
316
    {
317
        $validationResult = $customer->validate();
318
319
        if ($validationResult === true) {
320
            $this->_getMageModel()->getBillingAddress()->setEmail($customerRegisterData['email']);
321
            Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this->_getMageModel());
322
        }
323
    }
324
325
    protected function _setPaymentMethod()
326
    {
327
        $this->_validatePaymentMethod();
328
        $paymentMethodData = $this->_processFixtureAttributes($this->_data['fixture']['payment']);
329
        $this->_importPaymentData($paymentMethodData);
330
331
    }
332
333
    protected function _validatePaymentMethod()
334
    {
335
        if (!isset($this->_data['fixture']['payment']['method'])) {
336
            throw new UndefinedQuoteProducts(
337
                'Sales Order Fixture: Payment method has not been defined. Check fixture yml.'
338
            );
339
        }
340
    }
341
342
    protected function _importPaymentData(array $data)
343
    {
344
        $this->_getMageModel()->getPayment()->importData(['method' => $data['method']]);
345
    }
346
347
    /**
348
     * @throws UndefinedAttributes
349
     *
350
     */
351
    protected function _validateCheckoutMethod()
352
    {
353
        $this->_throwUndefinedAttributesException(
354
            isset($this->_data['fixture']['checkout']['method']),
355
            'Sales Order Fixture: Checkout method has not been defined. Check fixture yml.'
356
        );
357
    }
358
359
    protected function _saveFixture()
360
    {
361
        $this->_getMageModel()->save();
362
363
        $service = Mage::getModel('sales/service_quote', $this->_getMageModel());
364
        $service->submitAll();
365
366
        $order = $service->getOrder();
367
368
        return $order->getId();
369
370
    }
371
372
    /**
373
     * @throws UndefinedBundleProducts
374
     */
375
    protected function _throwUndefinedQuoteProductsException()
376
    {
377
        if (!isset($this->_data['fixture']['quote_products']['products'])) {
378
            throw new UndefinedQuoteProducts(
379
                'Sales Order Fixture: Quote products have not been defined. Check fixture yml.'
380
            );
381
        }
382
    }
383
384
    /**
385
     * @throws UndefinedQuoteAddresses
386
     *
387
     */
388
    protected function _throwUndefinedQuoteAddressesException()
389
    {
390
        if (!isset($this->_data['fixture']['addresses'])) {
391
            throw new UndefinedQuoteAddresses(
392
                'Sales Order Fixture: Quote addresses have not been defined. Check fixture yml.'
393
            );
394
        }
395
    }
396
}
397