Failed Conditions
Pull Request — develop (#1)
by Carlo
03:42
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
 * SalesOrder
4
 */
5
6
namespace Magefix\Fixture\Builder;
7
8
use Mage;
9
use Mage_Customer_Model_Group;
10
use Magefix\Exceptions\UndefinedAttributes;
11
use Magefix\Exceptions\UndefinedQuoteAddresses;
12
use Magefix\Exceptions\UndefinedQuoteProducts;
13
use Magefix\Exceptions\UnknownQuoteAddressType;
14
use Magefix\Fixture\Builder\Helper\Checkout;
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
    protected function _setCheckoutMethod()
183
    {
184
        $this->_validateCheckoutMethod();
185
        $checkoutMethodData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']);
186
        $this->_getMageModel()->setCheckoutMethod($checkoutMethodData['method']);
187
        $this->_setCheckoutMethodGuest($checkoutMethodData);
188
189
        if (Checkout::isRegisterCheckout($checkoutMethodData)) {
190
            $customerData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']['customer']);
191
            $this->_setCheckoutMethodRegister($customerData);
192
        }
193
    }
194
195
    /**
196
     * @param array $checkoutMethodData
197
     *
198
     */
199
    protected function _setCheckoutMethodGuest(array $checkoutMethodData)
200
    {
201
        if (Checkout::isGuestCheckout($checkoutMethodData)) {
0 ignored issues
show
Bug introduced by
The method isGuestCheckout() does not exist on Magefix\Fixture\Builder\Helper\Checkout. Did you maybe mean _isGuestCheckout()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
202
            $this->_getMageModel()->setCustomerId(null)
203
                ->setCustomerEmail($this->_getMageModel()->getBillingAddress()->getEmail())
204
                ->setCustomerIsGuest(true)
205
                ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
206
        }
207
    }
208
209
    /**
210
     * @param array $customerRegisterData
211
     *
212
     */
213
    protected function _setCheckoutMethodRegister(array $customerRegisterData)
214
    {
215
        $dob = $this->_setCustomerRegistrationOptionalData($customerRegisterData);
216
217
        $customer = Mage::getModel($customerRegisterData['model']);
218
        $this->_getMageModel()->setPasswordHash($customer->encryptPassword($customerRegisterData['password']));
219
        $customer->setData($customerRegisterData);
220
221
        if (isset($customerRegisterData['dob'])) {
222
            $customer->setDob($dob);
223
        }
224
225
        $this->_validateRegistrationCustomer($customerRegisterData, $customer);
226
    }
227
228
    /**
229
     * @param array $customerRegisterData
230
     *
231
     * @return date|bool
232
     *
233
     */
234
    protected function _setCustomerRegistrationOptionalData(array $customerRegisterData)
235
    {
236
        $dob = false;
237
        if (isset($customerRegisterData['dob'])) {
238
            $dob = Mage::app()->getLocale()->date($customerRegisterData['dob'], null, null, false)->toString(
239
                'yyyy-MM-dd'
240
            );
241
242
            $this->_getMageModel()->setCustomerDob($dob);
243
        }
244
245
        if (isset($customerRegisterData['taxvat'])) {
246
            $this->_getMageModel()->setCustomerTaxvat($customerRegisterData['taxvat']);
247
        }
248
249
        if (isset($customerRegisterData['gender'])) {
250
            $this->_getMageModel()->setCustomerGender($customerRegisterData['gender']);
251
        }
252
253
        return $dob;
254
    }
255
256
    /**
257
     * @param array $customerRegisterData
258
     * @param       $customer
259
     *
260
     */
261
    protected function _validateRegistrationCustomer(array $customerRegisterData, $customer)
262
    {
263
        $validationResult = $customer->validate();
264
265
        if ($validationResult === true) {
266
            $this->_getMageModel()->getBillingAddress()->setEmail($customerRegisterData['email']);
267
            Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this->_getMageModel());
268
        }
269
    }
270
271
    protected function _setPaymentMethod()
272
    {
273
        $this->_validatePaymentMethod();
274
        $paymentMethodData = $this->_processFixtureAttributes($this->_data['fixture']['payment']);
275
        $this->_importPaymentData($paymentMethodData);
276
277
    }
278
279
    protected function _importPaymentData(array $data)
280
    {
281
        $this->_getMageModel()->getPayment()->importData(['method' => $data['method']]);
282
    }
283
284
    /**
285
     * @return mixed
286
     * @throws Exception
287
     *
288
     */
289
    protected function _saveFixture()
290
    {
291
        $this->_getMageModel()->save();
292
        $order = Checkout::quoteServiceSubmitAll($this->_getMageModel());
293
294
        return $order->getId();
295
    }
296
297
    /**
298
     * @throws UndefinedQuoteProducts
299
     *
300
     */
301
    protected function _validatePaymentMethod()
302
    {
303
        if (!isset($this->_data['fixture']['payment']['method'])) {
304
            throw new UndefinedQuoteProducts(
305
                'Sales Order Fixture: Payment method has not been defined. Check fixture yml.'
306
            );
307
        }
308
    }
309
310
    /**
311
     * @throws UndefinedAttributes
312
     *
313
     */
314
    protected function _validateCheckoutMethod()
315
    {
316
        $this->_throwUndefinedAttributesException(
317
            isset($this->_data['fixture']['checkout']['method']),
318
            'Sales Order Fixture: Checkout method has not been defined. Check fixture yml.'
319
        );
320
    }
321
322
    /**
323
     * @throws UndefinedBundleProducts
324
     */
325
    protected function _throwUndefinedQuoteProductsException()
326
    {
327
        if (!isset($this->_data['fixture']['quote_products']['products'])) {
328
            throw new UndefinedQuoteProducts(
329
                'Sales Order Fixture: Quote products have not been defined. Check fixture yml.'
330
            );
331
        }
332
    }
333
334
    /**
335
     * @throws UndefinedQuoteAddresses
336
     *
337
     */
338
    protected function _throwUndefinedQuoteAddressesException()
339
    {
340
        if (!isset($this->_data['fixture']['addresses'])) {
341
            throw new UndefinedQuoteAddresses(
342
                'Sales Order Fixture: Quote addresses have not been defined. Check fixture yml.'
343
            );
344
        }
345
    }
346
}
347