Completed
Branch feature/MGFX-01-register-check... (bebeae)
by Carlo
05:24
created

SalesOrder::_setCustomerRegistrationOptionalData()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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