Completed
Push — 1.10 ( 93e51c...7f32fb )
by
unknown
11:06
created

LoadMagentoChannel::createTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture\CopyCustomerAddressToContact;
4
5
use Doctrine\Common\DataFixtures\AbstractFixture;
6
use Doctrine\Common\Persistence\ObjectManager;
7
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
use Oro\Bundle\AddressBundle\Entity\Address;
12
use Oro\Component\Config\Common\ConfigObject;
13
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
14
use Oro\Bundle\OrganizationBundle\Entity\Organization;
15
use Oro\Bundle\UserBundle\Entity\User;
16
use Oro\Bundle\UserBundle\Model\Gender;
17
18
use OroCRM\Bundle\ContactBundle\Entity\Contact;
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\Customer;
24
use OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup;
25
use OroCRM\Bundle\MagentoBundle\Entity\MagentoSoapTransport;
26
use OroCRM\Bundle\MagentoBundle\Entity\Order;
27
use OroCRM\Bundle\MagentoBundle\Entity\OrderItem;
28
use OroCRM\Bundle\MagentoBundle\Entity\Store;
29
use OroCRM\Bundle\MagentoBundle\Entity\Website;
30
use OroCRM\Bundle\MagentoBundle\Provider\Transport\SoapTransport;
31
32
class LoadMagentoChannel extends AbstractFixture implements ContainerAwareInterface
33
{
34
    const CHANNEL_NAME = 'Magento channel';
35
    const CHANNEL_TYPE = 'magento';
36
37
    /** @var ObjectManager */
38
    protected $em;
39
40
    /** @var integration */
41
    protected $integration;
42
43
    /** @var MagentoSoapTransport */
44
    protected $transport;
45
46
    /** @var array */
47
    protected $countries;
48
49
    /** @var array */
50
    protected $regions;
51
52
    /** @var Website */
53
    protected $website;
54
55
    /** @var Store */
56
    protected $store;
57
58
    /** @var CustomerGroup */
59
    protected $customerGroup;
60
61
    /** @var Channel */
62
    protected $channel;
63
64
    /** @var BuilderFactory */
65
    protected $factory;
66
67
    /** @var Organization */
68
    protected $organization;
69
70
    /** @var  User */
71
    protected $user;
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function setContainer(ContainerInterface $container = null)
77
    {
78
        $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...
79
    }
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function load(ObjectManager $manager)
84
    {
85
        $this->em        = $manager;
86
        $this->countries = $this->loadStructure('OroAddressBundle:Country', 'getIso2Code');
87
        $this->regions   = $this->loadStructure('OroAddressBundle:Region', 'getCombinedCode');
88
        $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
89
        $this
90
            ->createTransport()
91
            ->createIntegration()
92
            ->createChannel()
93
            ->createWebSite()
94
            ->createCustomerGroup()
95
            ->createGuestCustomerGroup()
96
            ->createStore();
97
        $account        = $this->createAccount();
98
        $this->setReference('account', $account);
99
        for ($i = 1; $i <= 5; $i++) {
100
            $magentoAddress = $this->createMagentoAddress($this->regions['US-AZ'], $this->countries['US']);
101
            $customer       = $this->createCustomer($i, $account, $magentoAddress);
102
            $this->setReference('customer_' . $i, $customer);
103
        }
104
        $this->setReference('integration', $this->integration);
105
        $this->em->flush();
106
    }
107
    /**
108
     * @param $table
109
     * @param $method
110
     *
111
     * @return array
112
     */
113 View Code Duplication
    protected function loadStructure($table, $method)
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...
114
    {
115
        $result   = [];
116
        $response = $this->em->getRepository($table)->findAll();
117
        foreach ($response as $row) {
118
            $result[call_user_func([$row, $method])] = $row;
119
        }
120
121
        return $result;
122
    }
123
    /**
124
     * @return $this
125
     */
126 View Code Duplication
    protected function createIntegration()
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...
127
    {
128
        $integration = new Integration();
129
        $integration->setName('Demo Web store');
130
        $integration->setType('magento');
131
        $integration->setConnectors(['customer', 'order', 'cart']);
132
        $integration->setTransport($this->transport);
133
        $integration->setOrganization($this->organization);
134
        $synchronizationSettings = ConfigObject::create(['isTwoWaySyncEnabled' => true]);
135
        $integration->setSynchronizationSettings($synchronizationSettings);
136
        $this->em->persist($integration);
137
        $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...sToContact\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...
138
139
        return $this;
140
    }
141
    /**
142
     * @return $this
143
     */
144 View Code Duplication
    protected function createTransport()
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...
145
    {
146
        $transport = new MagentoSoapTransport;
147
        $transport->setAdminUrl('http://localhost/magento/admin');
148
        $transport->setApiKey('key');
149
        $transport->setApiUser('user');
150
        $transport->setIsExtensionInstalled(true);
151
        $transport->setExtensionVersion(SoapTransport::REQUIRED_EXTENSION_VERSION);
152
        $transport->setMagentoVersion('1.9.1.0');
153
        $transport->setIsWsiMode(false);
154
        $transport->setWebsiteId('1');
155
        $transport->setWsdlUrl('http://localhost/magento/api/v2_soap?wsdl=1');
156
        $transport->setWebsites([['id' => 1, 'label' => 'Website ID: 1, Stores: English, French, German']]);
157
        $this->em->persist($transport);
158
        $this->transport = $transport;
159
160
        return $this;
161
    }
162
    /**
163
     * @param $region
164
     * @param $country
165
     *
166
     * @return MagentoAddress
167
     */
168 View Code Duplication
    protected function createMagentoAddress($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...
169
    {
170
        $address = new MagentoAddress;
171
        $address->setRegion($region);
172
        $address->setCountry($country);
173
        $address->setCity('City');
174
        $address->setStreet('street');
175
        $address->setPostalCode(123456);
176
        $address->setFirstName('John');
177
        $address->setLastName('Doe');
178
        $address->setLabel('label');
179
        $address->setPrimary(true);
180
        $address->setOrganization('oro');
181
        $address->setOriginId(1);
182
        $address->setChannel($this->integration);
183
        $address->setOrganization($this->organization);
184
        $address->setPrimary(true);
185
186
        $this->em->persist($address);
187
188
        return $address;
189
    }
190
    /**
191
     * @param $region
192
     * @param $country
193
     *
194
     * @return Address
195
     */
196
    protected function createAddress($region, $country)
197
    {
198
        $address = new Address;
199
        $address->setRegion($region);
200
        $address->setCountry($country);
201
        $address->setCity('City');
202
        $address->setStreet('street');
203
        $address->setPostalCode(123456);
204
        $address->setFirstName('John');
205
        $address->setLastName('Doe');
206
        $address->setOrganization($this->organization);
207
        $this->em->persist($address);
208
209
        return $address;
210
    }
211
    /**
212
     * @param                $oid
213
     * @param Account        $account
214
     * @param MagentoAddress $address
215
     *
216
     * @return Customer
217
     */
218
    protected function createCustomer($oid, Account $account, MagentoAddress $address)
219
    {
220
        $customer = new Customer();
221
        $customer->setChannel($this->integration);
222
        $customer->setDataChannel($this->channel);
223
        $customer->setFirstName('John ' . $oid);
224
        $customer->setLastName('Doe ' . $oid);
225
        $customer->setEmail('test' . $oid . '@example.com');
226
        $customer->setOriginId($oid);
227
        $customer->setIsActive(true);
228
        $customer->setWebsite($this->website);
229
        $customer->setStore($this->store);
230
        $customer->setAccount($account);
231
        $customer->setGender(Gender::MALE);
232
        $customer->setGroup($this->customerGroup);
233
        // TODO: DateTimeZones should be removed in BAP-8710. Tests should be passed for:
234
        //  - Oro\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest\CustomerControllerTest
235
        //  - Oro\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest\MagentoCustomerControllerTest
236
        $customer->setCreatedAt(new \DateTime('now', new \DateTimezone('UTC')));
237
        $customer->setUpdatedAt(new \DateTime('now', new \DateTimezone('UTC')));
238
        $customer->addAddress($address);
239
        $customer->setOwner($this->getUser());
240
        $customer->setOrganization($this->organization);
241
        $contact = $this->createContact($customer);
242
        $customer->setContact($contact);
243
        $this->em->persist($customer);
244
245
        return $customer;
246
    }
247
    /**
248
     * @param Customer $customer
249
     *
250
     * @return Contact
251
     */
252 View Code Duplication
    protected function createContact(Customer $customer)
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...
253
    {
254
        $contact = new Contact();
255
        $contact->setFirstName($customer->getFirstName());
256
        $contact->setLastName($customer->getLastName());
257
        $contact->setGender($customer->getGender());
258
        $contact->setOwner($this->getUser());
259
        $contact->setOrganization($this->organization);
260
        $this->em->persist($contact);
261
262
        return $contact;
263
    }
264
    /**
265
     * @return $this
266
     */
267
    protected function createWebSite()
268
    {
269
        $website = new Website();
270
        $website->setName('web site');
271
        $website->setOriginId(1);
272
        $website->setCode('web site code');
273
        $website->setChannel($this->integration);
274
        $this->setReference('website', $website);
275
        $this->em->persist($website);
276
        $this->website = $website;
277
278
        return $this;
279
    }
280
    /**
281
     * @return $this
282
     */
283
    protected function createStore()
284
    {
285
        $store = new Store;
286
        $store->setName('demo store');
287
        $store->setChannel($this->integration);
288
        $store->setCode(1);
289
        $store->setWebsite($this->website);
290
        $store->setOriginId(1);
291
        $this->em->persist($store);
292
        $this->store = $store;
293
        $this->setReference('store', $store);
294
295
        return $this;
296
    }
297
    /**
298
     * @return Account
299
     */
300 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...
301
    {
302
        $account = new Account;
303
        $account->setName('acc');
304
        $account->setOwner($this->getUser());
305
        $account->setOrganization($this->organization);
306
        $this->em->persist($account);
307
308
        return $account;
309
    }
310
    /**
311
     * @return $this
312
     */
313 View Code Duplication
    protected function createCustomerGroup()
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...
314
    {
315
        $customerGroup = new CustomerGroup;
316
        $customerGroup->setName('group');
317
        $customerGroup->setChannel($this->integration);
318
        $customerGroup->setOriginId(1);
319
        $this->em->persist($customerGroup);
320
        $this->setReference('customer_group', $customerGroup);
321
        $this->customerGroup = $customerGroup;
322
323
        return $this;
324
    }
325
    /**
326
     * @return $this
327
     */
328 View Code Duplication
    protected function createGuestCustomerGroup()
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...
329
    {
330
        $customerGroup = new CustomerGroup;
331
        $customerGroup->setName('NOT LOGGED IN');
332
        $customerGroup->setChannel($this->integration);
333
        $customerGroup->setOriginId(0);
334
        $this->em->persist($customerGroup);
335
336
        return $this;
337
    }
338
    /**
339
     * @param Order $order
340
     *
341
     * @return OrderItem
342
     */
343 View Code Duplication
    protected function createBaseOrderItem(Order $order)
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...
344
    {
345
        $orderItem = new OrderItem();
346
        $orderItem->setId(mt_rand(0, 9999));
347
        $orderItem->setName('some order item');
348
        $orderItem->setSku('some sku');
349
        $orderItem->setQty(1);
350
        $orderItem->setOrder($order);
351
        $orderItem->setCost(51.00);
352
        $orderItem->setPrice(75.00);
353
        $orderItem->setWeight(6.12);
354
        $orderItem->setTaxPercent(2);
355
        $orderItem->setTaxAmount(1.5);
356
        $orderItem->setDiscountPercent(4);
357
        $orderItem->setDiscountAmount(0);
358
        $orderItem->setRowTotal(234);
359
        $orderItem->setOwner($this->organization);
360
        $this->em->persist($orderItem);
361
362
        return $orderItem;
363
    }
364
    /**
365
     * @return User
366
     */
367 View Code Duplication
    protected function getUser()
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...
368
    {
369
        if (!$this->user) {
370
            $this->user = $this->em->getRepository('OroUserBundle:User')->findOneBy(['username' => 'admin']);
371
        }
372
373
        return $this->user;
374
    }
375
    /**
376
     * @return LoadMagentoChannel
377
     */
378 View Code Duplication
    protected function createChannel()
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...
379
    {
380
        $channel = $this
381
            ->factory
382
            ->createBuilder()
383
            ->setName(self::CHANNEL_NAME)
384
            ->setChannelType(self::CHANNEL_TYPE)
385
            ->setStatus(Channel::STATUS_ACTIVE)
386
            ->setDataSource($this->integration)
387
            ->setOwner($this->organization)
388
            ->setEntities()
389
            ->getChannel();
390
        $this->em->persist($channel);
391
        $this->em->flush();
392
        $this->setReference('default_channel', $channel);
393
        $this->channel = $channel;
394
395
        return $this;
396
    }
397
}
398