Completed
Push — 1.9 ( 30c3e5...ca8b96 )
by
unknown
42:25
created

LoadMagentoChannel::createWebSite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\DataFixtures\AbstractFixture;
7
use Doctrine\Common\Persistence\ObjectManager;
8
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
use Oro\Bundle\AddressBundle\Entity\Address;
13
use Oro\Bundle\DataGridBundle\Common\Object;
14
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
15
use Oro\Bundle\OrganizationBundle\Entity\Organization;
16
use Oro\Bundle\UserBundle\Entity\User;
17
use Oro\Bundle\UserBundle\Model\Gender;
18
19
use OroCRM\Bundle\AccountBundle\Entity\Account;
20
use OroCRM\Bundle\ChannelBundle\Builder\BuilderFactory;
21
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
22
use OroCRM\Bundle\MagentoBundle\Entity\Address as MagentoAddress;
23
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
24
use OroCRM\Bundle\MagentoBundle\Entity\CartAddress;
25
use OroCRM\Bundle\MagentoBundle\Entity\CartItem;
26
use OroCRM\Bundle\MagentoBundle\Entity\CartStatus;
27
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
28
use OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup;
29
use OroCRM\Bundle\MagentoBundle\Entity\MagentoSoapTransport;
30
use OroCRM\Bundle\MagentoBundle\Entity\Order;
31
use OroCRM\Bundle\MagentoBundle\Entity\OrderItem;
32
use OroCRM\Bundle\MagentoBundle\Entity\Store;
33
use OroCRM\Bundle\MagentoBundle\Entity\Website;
34
use OroCRM\Bundle\MagentoBundle\Provider\Transport\SoapTransport;
35
36
class LoadMagentoChannel extends AbstractFixture implements ContainerAwareInterface
37
{
38
    const CHANNEL_NAME = 'Magento channel';
39
    const CHANNEL_TYPE = 'magento';
40
41
    /** @var ObjectManager */
42
    protected $em;
43
44
    /** @var integration */
45
    protected $integration;
46
47
    /** @var MagentoSoapTransport */
48
    protected $transport;
49
50
    /** @var array */
51
    protected $countries;
52
53
    /** @var array */
54
    protected $regions;
55
56
    /** @var Website */
57
    protected $website;
58
59
    /** @var Store */
60
    protected $store;
61
62
    /** @var CustomerGroup */
63
    protected $customerGroup;
64
65
    /** @var Channel */
66
    protected $channel;
67
68
    /** @var BuilderFactory */
69
    protected $factory;
70
71
    /**
72
     * @var Organization
73
     */
74
    protected $organization;
75
76
    /**
77
     * {@inheritDoc}
78
     */
79
    public function setContainer(ContainerInterface $container = null)
80
    {
81
        $this->factory = $container->get('orocrm_channel.builder.factory');
0 ignored issues
show
Bug introduced by
It seems like $container is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    public function load(ObjectManager $manager)
88
    {
89
        $this->em        = $manager;
90
        $this->countries = $this->loadStructure('OroAddressBundle:Country', 'getIso2Code');
91
        $this->regions   = $this->loadStructure('OroAddressBundle:Region', 'getCombinedCode');
92
        $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
93
94
        $this
95
            ->createTransport()
96
            ->createIntegration()
97
            ->createChannel()
98
            ->createWebSite()
99
            ->createCustomerGroup()
100
            ->createStore();
101
102
        $magentoAddress = $this->createMagentoAddress($this->regions['US-AZ'], $this->countries['US']);
103
        $account        = $this->createAccount();
104
        $this->setReference('account', $account);
105
106
        $customer       = $this->createCustomer(1, $account, $magentoAddress);
107
        $cartAddress1   = $this->createCartAddress($this->regions['US-AZ'], $this->countries['US'], 1);
108
        $cartAddress2   = $this->createCartAddress($this->regions['US-AZ'], $this->countries['US'], 2);
109
        $cartItem       = $this->createCartItem();
110
        $status         = $this->getStatus();
111
        $items          = new ArrayCollection();
112
        $items->add($cartItem);
113
114
        $cart = $this->createCart($cartAddress1, $cartAddress2, $customer, $items, $status);
115
        $this->updateCartItem($cartItem, $cart);
116
117
        $order = $this->createOrder($cart, $customer);
118
119
        $this->setReference('customer', $customer);
120
        $this->setReference('integration', $this->integration);
121
        $this->setReference('cart', $cart);
122
        $this->setReference('order', $order);
123
124
        $baseOrderItem = $this->createBaseOrderItem($order);
125
        $order->setItems([$baseOrderItem]);
126
        $this->em->persist($order);
127
128
        $this->em->flush();
129
    }
130
131
    /**
132
     * @param                 $billing
133
     * @param                 $shipping
134
     * @param Customer        $customer
135
     * @param ArrayCollection $item
136
     * @param CartStatus      $status
137
     *
138
     * @return Cart
139
     */
140
    protected function createCart($billing, $shipping, Customer $customer, ArrayCollection $item, $status)
141
    {
142
        $cart = new Cart();
143
        $cart->setOriginId(100);
144
        $cart->setChannel($this->integration);
145
        $cart->setDataChannel($this->channel);
146
        $cart->setBillingAddress($billing);
147
        $cart->setShippingAddress($shipping);
148
        $cart->setCustomer($customer);
149
        $cart->setEmail('[email protected]');
150
        $cart->setCreatedAt(new \DateTime('now'));
151
        $cart->setUpdatedAt(new \DateTime('now'));
152
        $cart->setCartItems($item);
153
        $cart->setStatus($status);
154
        $cart->setItemsQty(0);
155
        $cart->setItemsCount(1);
156
        $cart->setBaseCurrencyCode('code');
157
        $cart->setStoreCurrencyCode('code');
158
        $cart->setQuoteCurrencyCode('usd');
159
        $cart->setStoreToBaseRate(12);
160
        $cart->setStoreToQuoteRate(12);
161
        $cart->setGrandTotal(2.54);
162
        $cart->setIsGuest(0);
163
        $cart->setStore($this->store);
164
        $cart->setOwner($this->getUser());
165
        $cart->setOrganization($this->organization);
166
167
        $this->em->persist($cart);
168
169
        return $cart;
170
    }
171
172
    /**
173
     * @param $table
174
     * @param $method
175
     *
176
     * @return array
177
     */
178
    protected function loadStructure($table, $method)
179
    {
180
        $result   = [];
181
        $response = $this->em->getRepository($table)->findAll();
182
        foreach ($response as $row) {
183
            $result[call_user_func([$row, $method])] = $row;
184
        }
185
186
        return $result;
187
    }
188
189
    /**
190
     * @return $this
191
     */
192
    protected function createIntegration()
193
    {
194
        $integration = new Integration();
195
        $integration->setName('Demo Web store');
196
        $integration->setType('magento');
197
        $integration->setConnectors(['customer', 'order', 'cart']);
198
        $integration->setTransport($this->transport);
199
        $integration->setOrganization($this->organization);
200
201
        $synchronizationSettings = Object::create(['isTwoWaySyncEnabled' => true]);
202
        $integration->setSynchronizationSettings($synchronizationSettings);
203
204
        $this->em->persist($integration);
205
        $this->integration = $integration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $integration of type object<Oro\Bundle\Integr...nBundle\Entity\Channel> is incompatible with the declared type object<OroCRM\Bundle\Mag...al\Fixture\integration> of property $integration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
206
207
        return $this;
208
    }
209
210
    /**
211
     * @return $this
212
     */
213
    protected function createTransport()
214
    {
215
        $transport = new MagentoSoapTransport;
216
        $transport->setAdminUrl('http://localhost/magento/admin');
217
        $transport->setApiKey('key');
218
        $transport->setApiUser('user');
219
        $transport->setIsExtensionInstalled(true);
220
        $transport->setExtensionVersion(SoapTransport::REQUIRED_EXTENSION_VERSION);
221
        $transport->setMagentoVersion('1.9.1.0');
222
        $transport->setIsWsiMode(false);
223
        $transport->setWebsiteId('1');
224
        $transport->setWsdlUrl('http://localhost/magento/api/v2_soap?wsdl=1');
225
        $transport->setWebsites([['id' => 1, 'label' => 'Website ID: 1, Stores: English, French, German']]);
226
227
        $this->em->persist($transport);
228
        $this->transport = $transport;
229
230
        return $this;
231
    }
232
233
    /**
234
     * @param $region
235
     * @param $country
236
     * @param $originId
237
     *
238
     * @return CartAddress
239
     */
240 View Code Duplication
    protected function createCartAddress($region, $country, $originId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        $cartAddress = new CartAddress;
243
        $cartAddress->setRegion($region);
244
        $cartAddress->setCountry($country);
245
        $cartAddress->setCity('City');
246
        $cartAddress->setStreet('street');
247
        $cartAddress->setPostalCode(123456);
248
        $cartAddress->setFirstName('John');
249
        $cartAddress->setLastName('Doe');
250
        $cartAddress->setOriginId($originId);
251
        $cartAddress->setOrganization($this->organization);
252
253
        $this->em->persist($cartAddress);
254
255
        return $cartAddress;
256
    }
257
258
    /**
259
     * @param $region
260
     * @param $country
261
     *
262
     * @return MagentoAddress
263
     */
264
    protected function createMagentoAddress($region, $country)
265
    {
266
        $address = new MagentoAddress;
267
        $address->setRegion($region);
268
        $address->setCountry($country);
269
        $address->setCity('City');
270
        $address->setStreet('street');
271
        $address->setPostalCode(123456);
272
        $address->setFirstName('John');
273
        $address->setLastName('Doe');
274
        $address->setLabel('label');
275
        $address->setPrimary(true);
276
        $address->setOrganization('oro');
277
        $address->setOriginId(1);
278
        $address->setChannel($this->integration);
279
        $address->setOrganization($this->organization);
280
281
        $this->em->persist($address);
282
283
        return $address;
284
    }
285
286
    /**
287
     * @param $region
288
     * @param $country
289
     *
290
     * @return Address
291
     */
292 View Code Duplication
    protected function createAddress($region, $country)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
    {
294
        $address = new Address;
295
        $address->setRegion($region);
296
        $address->setCountry($country);
297
        $address->setCity('City');
298
        $address->setStreet('street');
299
        $address->setPostalCode(123456);
300
        $address->setFirstName('John');
301
        $address->setLastName('Doe');
302
        $address->setOrganization($this->organization);
303
304
        $this->em->persist($address);
305
306
        return $address;
307
    }
308
309
    /**
310
     * @param                $oid
311
     * @param Account        $account
312
     * @param MagentoAddress $address
313
     *
314
     * @return Customer
315
     */
316
    protected function createCustomer($oid, Account $account, MagentoAddress $address)
317
    {
318
        $customer = new Customer();
319
        $customer->setChannel($this->integration);
320
        $customer->setDataChannel($this->channel);
321
        $customer->setFirstName('John');
322
        $customer->setLastName('Doe');
323
        $customer->setEmail('[email protected]');
324
        $customer->setOriginId($oid);
325
        $customer->setIsActive(true);
326
        $customer->setWebsite($this->website);
327
        $customer->setStore($this->store);
328
        $customer->setAccount($account);
329
        $customer->setGender(Gender::MALE);
330
        $customer->setGroup($this->customerGroup);
331
        // TODO: DateTimeZones should be removed in BAP-8710. Tests should be passed for:
332
        //  - OroCRM\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest\CustomerControllerTest
333
        //  - OroCRM\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest\MagentoCustomerControllerTest
334
        $customer->setCreatedAt(new \DateTime('now', new \DateTimezone('UTC')));
335
        $customer->setUpdatedAt(new \DateTime('now', new \DateTimezone('UTC')));
336
        $customer->addAddress($address);
337
        $customer->setOwner($this->getUser());
338
        $customer->setOrganization($this->organization);
339
340
        $this->em->persist($customer);
341
342
        return $customer;
343
    }
344
345
    /**
346
     * @return $this
347
     */
348 View Code Duplication
    protected function createWebSite()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
349
    {
350
        $website = new Website();
351
        $website->setName('web site');
352
        $website->setOriginId(1);
353
        $website->setCode('web site code');
354
        $website->setChannel($this->integration);
355
356
        $this->setReference('website', $website);
357
        $this->em->persist($website);
358
        $this->website = $website;
359
360
        return $this;
361
    }
362
363
    /**
364
     * @return $this
365
     */
366 View Code Duplication
    protected function createStore()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
367
    {
368
        $store = new Store;
369
        $store->setName('demo store');
370
        $store->setChannel($this->integration);
371
        $store->setCode(1);
372
        $store->setWebsite($this->website);
373
        $store->setOriginId(1);
374
375
        $this->em->persist($store);
376
        $this->store = $store;
377
        $this->setReference('store', $store);
378
379
        return $this;
380
    }
381
382
    /**
383
     * @return Account
384
     */
385 View Code Duplication
    protected function createAccount()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
386
    {
387
        $account = new Account;
388
        $account->setName('acc');
389
        $account->setOwner($this->getUser());
390
        $account->setOrganization($this->organization);
391
392
        $this->em->persist($account);
393
394
        return $account;
395
    }
396
397
    /**
398
     * @return $this
399
     */
400
    protected function createCustomerGroup()
401
    {
402
        $customerGroup = new CustomerGroup;
403
        $customerGroup->setName('group');
404
        $customerGroup->setChannel($this->integration);
405
        $customerGroup->setOriginId(1);
406
407
        $this->em->persist($customerGroup);
408
        $this->setReference('customer_group', $customerGroup);
409
        $this->customerGroup = $customerGroup;
410
411
        return $this;
412
    }
413
414
    /**
415
     * @return CartItem
416
     */
417
    protected function createCartItem()
418
    {
419
        $cartItem = new CartItem();
420
        $cartItem->setName('item' . mt_rand(0, 99999));
421
        $cartItem->setDescription('something');
422
        $cartItem->setPrice(mt_rand(10, 99999));
423
        $cartItem->setProductId(1);
424
        $cartItem->setFreeShipping('true');
425
        $cartItem->setIsVirtual(1);
426
        $cartItem->setRowTotal(100);
427
        $cartItem->setTaxAmount(10);
428
        $cartItem->setProductType('type');
429
        $cartItem->setSku('sku');
430
        $cartItem->setQty(0);
431
        $cartItem->setDiscountAmount(0);
432
        $cartItem->setTaxPercent(0);
433
        $cartItem->setCreatedAt(new \DateTime('now'));
434
        $cartItem->setUpdatedAt(new \DateTime('now'));
435
        $cartItem->setOwner($this->organization);
436
437
        $this->em->persist($cartItem);
438
439
        return $cartItem;
440
    }
441
442
    /**
443
     * @return CartStatus
444
     */
445
    protected function getStatus()
446
    {
447
        $status = $this->em->getRepository('OroCRMMagentoBundle:CartStatus')->findOneBy(['name' => 'open']);
448
449
        return $status;
450
    }
451
452
    /**
453
     * @param CartItem $cartItem
454
     * @param Cart     $cart
455
     *
456
     * @return $this
457
     */
458
    protected function updateCartItem(CartItem $cartItem, Cart $cart)
459
    {
460
        $cartItem->setCart($cart);
461
        $this->em->persist($cartItem);
462
463
        return $this;
464
    }
465
466
    /**
467
     * @param Cart     $cart
468
     * @param Customer $customer
469
     *
470
     * @return Order
471
     */
472
    protected function createOrder(Cart $cart, Customer $customer)
473
    {
474
        $order = new Order();
475
        $order->setChannel($this->integration);
476
        $order->setDataChannel($this->channel);
477
        $order->setStatus('open');
478
        $order->setIncrementId('100000307');
479
        $order->setCreatedAt(new \DateTime('now'));
480
        $order->setUpdatedAt(new \DateTime('now'));
481
        $order->setCart($cart);
482
        $order->setStore($this->store);
483
        $order->setCustomer($customer);
484
        $order->setCustomerEmail('[email protected]');
485
        $order->setDiscountAmount(4.40);
486
        $order->setTaxAmount(12.47);
487
        $order->setShippingAmount(5);
488
        $order->setTotalPaidAmount(17.85);
489
        $order->setTotalInvoicedAmount(11);
490
        $order->setTotalRefundedAmount(4);
491
        $order->setTotalCanceledAmount(0);
492
        $order->setShippingMethod('some unique shipping method');
493
        $order->setRemoteIp('127.0.0.1');
494
        $order->setGiftMessage('some very unique gift message');
495
        $order->setOwner($this->getUser());
496
        $order->setOrganization($this->organization);
497
498
        $this->em->persist($order);
499
500
        return $order;
501
    }
502
503
    /**
504
     * @param Order $order
505
     * @return OrderItem
506
     */
507
    protected function createBaseOrderItem(Order $order)
508
    {
509
        $orderItem = new OrderItem();
510
        $orderItem->setId(mt_rand(0, 9999));
511
        $orderItem->setName('some order item');
512
        $orderItem->setSku('some sku');
513
        $orderItem->setQty(1);
514
        $orderItem->setOrder($order);
515
        $orderItem->setCost(51.00);
516
        $orderItem->setPrice(75.00);
517
        $orderItem->setWeight(6.12);
518
        $orderItem->setTaxPercent(2);
519
        $orderItem->setTaxAmount(1.5);
520
        $orderItem->setDiscountPercent(4);
521
        $orderItem->setDiscountAmount(0);
522
        $orderItem->setRowTotal(234);
523
        $orderItem->setOwner($this->organization);
524
525
        $this->em->persist($orderItem);
526
527
        return $orderItem;
528
    }
529
530
    /**
531
     * @return User
532
     */
533
    protected function getUser()
534
    {
535
        $user = $this->em->getRepository('OroUserBundle:User')->findOneBy(['username' => 'admin']);
536
537
        return $user;
538
    }
539
540
    /**
541
     * @return LoadMagentoChannel
542
     */
543
    protected function createChannel()
544
    {
545
        $channel = $this
546
            ->factory
547
            ->createBuilder()
548
            ->setName(self::CHANNEL_NAME)
549
            ->setChannelType(self::CHANNEL_TYPE)
550
            ->setStatus(Channel::STATUS_ACTIVE)
551
            ->setDataSource($this->integration)
552
            ->setOwner($this->organization)
553
            ->setEntities()
554
            ->getChannel();
555
556
        $this->em->persist($channel);
557
        $this->em->flush();
558
559
        $this->setReference('default_channel', $channel);
560
561
        $this->channel = $channel;
562
563
        return $this;
564
    }
565
}
566