Failed Conditions
Pull Request — develop (#1)
by Carlo
05:06
created

SalesOrder::_isCheckoutMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
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 10
rs 9.4286
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
286
     *
287
     */
288
    protected function _setCustomerRegistrationOptionalData(array $customerRegisterData)
289
    {
290
        if (isset($customerRegisterData['dob'])) {
291
            $dob = Mage::app()->getLocale()->date($customerRegisterData['dob'], null, null, false)->toString(
292
                'yyyy-MM-dd'
293
            );
294
295
            $this->_getMageModel()->setCustomerDob($dob);
296
        }
297
298
        if (isset($customerRegisterData['taxvat'])) {
299
            $this->_getMageModel()->setCustomerTaxvat($customerRegisterData['taxvat']);
300
        }
301
302
        if (isset($customerRegisterData['gender'])) {
303
            $this->_getMageModel()->setCustomerGender($customerRegisterData['gender']);
304
        }
305
306
        return $dob;
0 ignored issues
show
Bug introduced by
The variable $dob does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
307
    }
308
309
    /**
310
     * @param array $customerRegisterData
311
     * @param       $customer
312
     *
313
     */
314
    protected function _validateRegistrationCustomer(array $customerRegisterData, $customer)
315
    {
316
        $validationResult = $customer->validate();
317
318
        if ($validationResult === true) {
319
            $this->_getMageModel()->getBillingAddress()->setEmail($customerRegisterData['email']);
320
            Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this->_getMageModel());
321
        }
322
    }
323
324
    protected function _setPaymentMethod()
325
    {
326
        $this->_validatePaymentMethod();
327
        $paymentMethodData = $this->_processFixtureAttributes($this->_data['fixture']['payment']);
328
        $this->_importPaymentData($paymentMethodData);
329
330
    }
331
332
    protected function _validatePaymentMethod()
333
    {
334
        if (!isset($this->_data['fixture']['payment']['method'])) {
335
            throw new UndefinedQuoteProducts(
336
                'Sales Order Fixture: Payment method has not been defined. Check fixture yml.'
337
            );
338
        }
339
    }
340
341
    protected function _importPaymentData(array $data)
342
    {
343
        $this->_getMageModel()->getPayment()->importData(['method' => $data['method']]);
344
    }
345
346
    /**
347
     * @throws UndefinedAttributes
348
     *
349
     */
350
    protected function _validateCheckoutMethod()
351
    {
352
        $this->_throwUndefinedAttributesException(
353
            isset($this->_data['fixture']['checkout']['method']),
354
            'Sales Order Fixture: Checkout method has not been defined. Check fixture yml.'
355
        );
356
    }
357
358
    protected function _saveFixture()
359
    {
360
        $this->_getMageModel()->save();
361
362
        $service = Mage::getModel('sales/service_quote', $this->_getMageModel());
363
        $service->submitAll();
364
365
        $order = $service->getOrder();
366
367
        return $order->getId();
368
369
    }
370
371
    /**
372
     * @throws UndefinedBundleProducts
373
     */
374
    protected function _throwUndefinedQuoteProductsException()
375
    {
376
        if (!isset($this->_data['fixture']['quote_products']['products'])) {
377
            throw new UndefinedQuoteProducts(
378
                'Sales Order Fixture: Quote products have not been defined. Check fixture yml.'
379
            );
380
        }
381
    }
382
383
    /**
384
     * @throws UndefinedQuoteAddresses
385
     *
386
     */
387
    protected function _throwUndefinedQuoteAddressesException()
388
    {
389
        if (!isset($this->_data['fixture']['addresses'])) {
390
            throw new UndefinedQuoteAddresses(
391
                'Sales Order Fixture: Quote addresses have not been defined. Check fixture yml.'
392
            );
393
        }
394
    }
395
}
396