Completed
Pull Request — develop (#1)
by Carlo
04:41
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
use Magefix\Fixture\Builder\Helper\ShippingAddress;
13
use Magefix\Fixture\Builder\Helper\ShippingMethod;
14
15
/**
16
 * Class SalesOrder
17
 *
18
 * @package Magefix\Fixture\Builder
19
 * @author  Carlo Tasca <[email protected]>
20
 */
21
class SalesOrder extends AbstractBuilder
22
{
23
    /**
24
     * @var array
25
     */
26
    private $_quoteProducts = [];
27
28
    /**
29
     * @throws UndefinedAttributes
30
     * @throws UnknownQuoteAddressType
31
     *
32
     */
33
    public function build()
34
    {
35
        $defaultData = $this->_getMageModelData() ? $this->_getMageModelData() : [];
36
        $fixtureData = $this->_getFixtureAttributes();
37
        $mergedData  = array_merge($defaultData, $fixtureData);
38
39
        $this->_getMageModel()->setData($mergedData);
40
        $this->_buildQuoteProductFixtures();
41
        $this->_addProductsToQuote();
42
        $this->_addAddressesToQuote();
43
        $this->_setShippingMethod();
44
        $this->_setCheckoutMethod();
45
        $this->_setPaymentMethod();
46
47
        return $this->_saveFixture();
48
    }
49
50
    /**
51
     * @throws UndefinedQuoteProducts
52
     * @throws \Magefix\Fixture\Factory\UndefinedFixtureModel
53
     *
54
     */
55
    protected function _buildQuoteProductFixtures()
56
    {
57
        $this->_throwUndefinedQuoteProductsException();
58
        $this->_quoteProducts = $this->_buildManySimple('quote_products');
59
    }
60
61
62
    protected function _addProductsToQuote()
63
    {
64
        foreach ($this->_quoteProducts as $product) {
65
            $this->_getMageModel()->addProduct($product);
66
        }
67
    }
68
69
    /**
70
     * @throws UndefinedQuoteAddresses
71
     * @throws UnknownQuoteAddressType
72
     *
73
     */
74
    protected function _addAddressesToQuote()
75
    {
76
        $this->_throwUndefinedQuoteAddressesException();
77
78
        $addresses = new ShippingAddress($this, $this->_getMageModel(), $this->_data);
79
        $addresses->addToQuote();
80
    }
81
82
    protected function _setShippingMethod()
83
    {
84
        $this->_validateShippingMethodData();
85
86
        $shippingData = $this->_processFixtureAttributes($this->_data['fixture']['shipping_method']);
87
88
        $shippingMethod = new ShippingMethod($this->_getMageModel(), $shippingData);
89
        $shippingMethod->addShippingDataToQuote();
90
    }
91
92
    /**
93
     * @throws UndefinedAttributes
94
     *
95
     */
96
    protected function _validateShippingMethodData()
97
    {
98
        $this->_throwUndefinedAttributesException(
99
            isset($this->_data['fixture']['shipping_method']['carrier']),
100
            'Sales Order Fixture: Shipping carrier has not been defined. Check fixture yml.'
101
        );
102
103
        $this->_throwUndefinedAttributesException(
104
            isset($this->_data['fixture']['shipping_method']['method']),
105
            'Sales Order Fixture: Shipping method has not been defined. Check fixture yml.'
106
        );
107
108
        $this->_throwUndefinedAttributesException(
109
            isset($this->_data['fixture']['shipping_method']['free_shipping']),
110
            'Sales Order Fixture: Free shipping has not been defined. Check fixture yml.'
111
        );
112
113
        $this->_throwUndefinedAttributesException(
114
            isset($this->_data['fixture']['shipping_method']['collect_totals']),
115
            'Sales Order Fixture: Collect totals has not been defined. Check fixture yml.'
116
        );
117
118
        $this->_throwUndefinedAttributesException(
119
            isset($this->_data['fixture']['shipping_method']['collect_shipping_rates']),
120
            'Sales Order Fixture: Collect shipping rates has not been defined. Check fixture yml.'
121
        );
122
    }
123
124
    protected function _setCheckoutMethod()
125
    {
126
        $this->_validateCheckoutMethod();
127
        $checkoutMethodData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']);
128
        $this->_getMageModel()->setCheckoutMethod($checkoutMethodData['method']);
129
        $this->_setCheckoutMethodGuest($checkoutMethodData);
130
131
        if (Checkout::isRegisterCheckout($checkoutMethodData)) {
132
            $customerData = $this->_processFixtureAttributes($this->_data['fixture']['checkout']['customer']);
133
            $this->_setCheckoutMethodRegister($customerData);
134
        }
135
    }
136
137
    /**
138
     * @param array $checkoutMethodData
139
     *
140
     */
141
    protected function _setCheckoutMethodGuest(array $checkoutMethodData)
142
    {
143
        if (Checkout::isGuestCheckout($checkoutMethodData)) {
144
            $this->_getMageModel()->setCustomerId(null)
145
                ->setCustomerEmail($this->_getMageModel()->getBillingAddress()->getEmail())
146
                ->setCustomerIsGuest(true)
147
                ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
148
        }
149
    }
150
151
    /**
152
     * @param array $customerRegisterData
153
     *
154
     */
155
    protected function _setCheckoutMethodRegister(array $customerRegisterData)
156
    {
157
        $dob = $this->_setCustomerRegistrationOptionalData($customerRegisterData);
158
159
        $customer = Mage::getModel($customerRegisterData['model']);
160
        $this->_getMageModel()->setPasswordHash($customer->encryptPassword($customerRegisterData['password']));
161
        $customer->setData($customerRegisterData);
162
163
        if (isset($customerRegisterData['dob'])) {
164
            $customer->setDob($dob);
165
        }
166
167
        $this->_validateRegistrationCustomer($customerRegisterData, $customer);
168
    }
169
170
    /**
171
     * @param array $customerRegisterData
172
     *
173
     * @return date|bool
174
     *
175
     */
176
    protected function _setCustomerRegistrationOptionalData(array $customerRegisterData)
177
    {
178
        $dob = false;
179
        if (isset($customerRegisterData['dob'])) {
180
            $dob = Mage::app()->getLocale()->date($customerRegisterData['dob'], null, null, false)->toString(
181
                'yyyy-MM-dd'
182
            );
183
184
            $this->_getMageModel()->setCustomerDob($dob);
185
        }
186
187
        if (isset($customerRegisterData['taxvat'])) {
188
            $this->_getMageModel()->setCustomerTaxvat($customerRegisterData['taxvat']);
189
        }
190
191
        if (isset($customerRegisterData['gender'])) {
192
            $this->_getMageModel()->setCustomerGender($customerRegisterData['gender']);
193
        }
194
195
        return $dob;
196
    }
197
198
    /**
199
     * @param array $customerRegisterData
200
     * @param       $customer
201
     *
202
     */
203
    protected function _validateRegistrationCustomer(array $customerRegisterData, $customer)
204
    {
205
        $validationResult = $customer->validate();
206
207
        if ($validationResult === true) {
208
            $this->_getMageModel()->getBillingAddress()->setEmail($customerRegisterData['email']);
209
            Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this->_getMageModel());
210
        }
211
    }
212
213
    protected function _setPaymentMethod()
214
    {
215
        $this->_validatePaymentMethod();
216
        $paymentMethodData = $this->_processFixtureAttributes($this->_data['fixture']['payment']);
217
        $this->_importPaymentData($paymentMethodData);
218
219
    }
220
221
    protected function _importPaymentData(array $data)
222
    {
223
        $this->_getMageModel()->getPayment()->importData(['method' => $data['method']]);
224
    }
225
226
    /**
227
     * @return mixed
228
     * @throws Exception
229
     *
230
     */
231
    protected function _saveFixture()
232
    {
233
        $this->_getMageModel()->save();
234
        $order = Checkout::quoteServiceSubmitAll($this->_getMageModel());
235
236
        return $order->getId();
237
    }
238
239
    /**
240
     * @throws UndefinedQuoteProducts
241
     *
242
     */
243
    protected function _validatePaymentMethod()
244
    {
245
        if (!isset($this->_data['fixture']['payment']['method'])) {
246
            throw new UndefinedQuoteProducts(
247
                'Sales Order Fixture: Payment method has not been defined. Check fixture yml.'
248
            );
249
        }
250
    }
251
252
    /**
253
     * @throws UndefinedAttributes
254
     *
255
     */
256
    protected function _validateCheckoutMethod()
257
    {
258
        $this->_throwUndefinedAttributesException(
259
            isset($this->_data['fixture']['checkout']['method']),
260
            'Sales Order Fixture: Checkout method has not been defined. Check fixture yml.'
261
        );
262
    }
263
264
    /**
265
     * @throws UndefinedBundleProducts
266
     */
267
    protected function _throwUndefinedQuoteProductsException()
268
    {
269
        if (!isset($this->_data['fixture']['quote_products']['products'])) {
270
            throw new UndefinedQuoteProducts(
271
                'Sales Order Fixture: Quote products have not been defined. Check fixture yml.'
272
            );
273
        }
274
    }
275
276
    /**
277
     * @throws UndefinedQuoteAddresses
278
     *
279
     */
280
    protected function _throwUndefinedQuoteAddressesException()
281
    {
282
        if (!isset($this->_data['fixture']['addresses'])) {
283
            throw new UndefinedQuoteAddresses(
284
                'Sales Order Fixture: Quote addresses have not been defined. Check fixture yml.'
285
            );
286
        }
287
    }
288
}
289