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

LoadMagentoChannel::createChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 19
rs 9.4285
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Tests\Functional\Fixture\ResyncMagentoCustomerAddresses;
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\Component\Config\Common\ConfigObject;
12
13
use Oro\Bundle\AddressBundle\Entity\Address;
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\ContactBundle\Entity\ContactAddress;
20
use OroCRM\Bundle\ContactBundle\Entity\Contact;
21
use OroCRM\Bundle\AccountBundle\Entity\Account;
22
use OroCRM\Bundle\ChannelBundle\Builder\BuilderFactory;
23
use OroCRM\Bundle\ChannelBundle\Entity\Channel;
24
use OroCRM\Bundle\MagentoBundle\Entity\Address as MagentoAddress;
25
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
26
use OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup;
27
use OroCRM\Bundle\MagentoBundle\Entity\MagentoSoapTransport;
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
    /**
82
     * {@inheritDoc}
83
     */
84
    public function load(ObjectManager $manager)
85
    {
86
        $this->em        = $manager;
87
        $this->countries = $this->loadStructure('OroAddressBundle:Country', 'getIso2Code');
88
        $this->regions   = $this->loadStructure('OroAddressBundle:Region', 'getCombinedCode');
89
        $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
90
        $this
91
            ->createTransport()
92
            ->createIntegration()
93
            ->createChannel()
94
            ->createWebSite()
95
            ->createCustomerGroup()
96
            ->createGuestCustomerGroup()
97
            ->createStore();
98
        $account        = $this->createAccount();
99
        $this->setReference('account', $account);
100
101
        $magentoCustomers = [
102
            [
103
                'key' => 'customer_without_address_and_contactAddress',
104
                'oid' => 1,
105
                'skipCreatingAddress' => true,
106
                'skipCreatingContactAddress' => true,
107
            ],
108
            [
109
                'key' => 'customer_without_address',
110
                'oid' => 2,
111
                'skipCreatingAddress' => true,
112
                'skipCreatingContactAddress' => false,
113
            ],
114
            [
115
                'key' => 'customer_without_contactAddress',
116
                'oid' => 3,
117
                'skipCreatingAddress' => false,
118
                'skipCreatingContactAddress' => true,
119
            ],
120
            [
121
                'key' => 'customer_with_address_and_contactAddress',
122
                'oid' => 4,
123
                'skipCreatingAddress' => false,
124
                'skipCreatingContactAddress' => false,
125
            ]
126
        ];
127
128
        foreach ($magentoCustomers as $magentoCustomer) {
129
            $magentoAddress = null;
130
            $contactAddress = null;
131
132
            if (!$magentoCustomer['skipCreatingAddress']) {
133
                $magentoAddress = $this->createMagentoAddress($this->regions['US-AZ'], $this->countries['US']);
134
            }
135
            if (!$magentoCustomer['skipCreatingContactAddress']) {
136
                $contactAddress = $this->createContactAddress($this->regions['US-AZ'], $this->countries['US']);
137
            }
138
            $customer       = $this->createCustomer(
139
                $magentoCustomer['oid'],
140
                $account,
141
                $magentoAddress,
142
                $contactAddress
143
            );
144
145
            $this->setReference($magentoCustomer['key'], $customer);
146
        }
147
148
        $this->setReference('integration', $this->integration);
149
        $this->em->flush();
150
    }
151
152
    /**
153
     * @param $table
154
     * @param $method
155
     *
156
     * @return array
157
     */
158 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...
159
    {
160
        $result   = [];
161
        $response = $this->em->getRepository($table)->findAll();
162
        foreach ($response as $row) {
163
            $result[call_user_func([$row, $method])] = $row;
164
        }
165
166
        return $result;
167
    }
168
169
    /**
170
     * @return $this
171
     */
172 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...
173
    {
174
        $integration = new Integration();
175
        $integration->setName('Demo Web store');
176
        $integration->setType('magento');
177
        $integration->setConnectors(['customer', 'order', 'cart']);
178
        $integration->setTransport($this->transport);
179
        $integration->setOrganization($this->organization);
180
        $synchronizationSettings = ConfigObject::create(['isTwoWaySyncEnabled' => true]);
181
        $integration->setSynchronizationSettings($synchronizationSettings);
182
        $this->em->persist($integration);
183
        $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...rAddresses\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...
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return $this
190
     */
191 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...
192
    {
193
        $transport = new MagentoSoapTransport;
194
        $transport->setAdminUrl('http://localhost/magento/admin');
195
        $transport->setApiKey('key');
196
        $transport->setApiUser('user');
197
        $transport->setIsExtensionInstalled(true);
198
        $transport->setExtensionVersion(SoapTransport::REQUIRED_EXTENSION_VERSION);
199
        $transport->setMagentoVersion('1.9.1.0');
200
        $transport->setIsWsiMode(false);
201
        $transport->setWebsiteId('1');
202
        $transport->setWsdlUrl('http://localhost/magento/api/v2_soap?wsdl=1');
203
        $transport->setWebsites([['id' => 1, 'label' => 'Website ID: 1, Stores: English, French, German']]);
204
        $this->em->persist($transport);
205
        $this->transport = $transport;
206
207
        return $this;
208
    }
209
210
    /**
211
     * @param $region
212
     * @param $country
213
     *
214
     * @return MagentoAddress
215
     */
216 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...
217
    {
218
        $address = new MagentoAddress;
219
        $address->setRegion($region);
220
        $address->setCountry($country);
221
        $address->setCity('City');
222
        $address->setStreet('street');
223
        $address->setPostalCode(123456);
224
        $address->setFirstName('John');
225
        $address->setLastName('Doe');
226
        $address->setLabel('label');
227
        $address->setPrimary(true);
228
        $address->setOrganization('oro');
229
        $address->setOriginId(1);
230
        $address->setChannel($this->integration);
231
        $address->setOrganization($this->organization);
232
        $address->setPrimary(true);
233
        $this->em->persist($address);
234
235
        return $address;
236
    }
237
238
    /**
239
     * @param $region
240
     * @param $country
241
     *
242
     * @return ContactAddress
243
     */
244
    protected function createContactAddress($region, $country)
245
    {
246
        $address = new ContactAddress();
247
        $address->setRegion($region);
248
        $address->setCountry($country);
249
        $address->setCity('City');
250
        $address->setStreet('street');
251
        $address->setPostalCode(123456);
252
        $address->setFirstName('John');
253
        $address->setLastName('Doe');
254
        $address->setLabel('label');
255
        $address->setPrimary(true);
256
        $address->setOrganization('oro');
257
        $address->setOrganization($this->organization);
258
        $this->em->persist($address);
259
260
        return $address;
261
    }
262
263
    /**
264
     * @param $region
265
     * @param $country
266
     *
267
     * @return Address
268
     */
269
    protected function createAddress($region, $country)
270
    {
271
        $address = new Address;
272
        $address->setRegion($region);
273
        $address->setCountry($country);
274
        $address->setCity('City');
275
        $address->setStreet('street');
276
        $address->setPostalCode(123456);
277
        $address->setFirstName('John');
278
        $address->setLastName('Doe');
279
        $address->setOrganization($this->organization);
280
        $this->em->persist($address);
281
282
        return $address;
283
    }
284
285
    /**
286
     * @param                $oid
287
     * @param Account        $account
288
     * @param MagentoAddress $address
289
     *
290
     * @return Customer
291
     */
292
    protected function createCustomer(
293
        $oid,
294
        Account $account,
295
        MagentoAddress $address = null,
296
        ContactAddress $contactAddress = null
297
    ) {
298
        $customer = new Customer();
299
        $customer->setChannel($this->integration);
300
        $customer->setDataChannel($this->channel);
301
        $customer->setFirstName('John ' . $oid);
302
        $customer->setLastName('Doe ' . $oid);
303
        $customer->setEmail('test' . $oid . '@example.com');
304
        $customer->setOriginId($oid);
305
        $customer->setIsActive(true);
306
        $customer->setWebsite($this->website);
307
        $customer->setStore($this->store);
308
        $customer->setAccount($account);
309
        $customer->setGender(Gender::MALE);
310
        $customer->setGroup($this->customerGroup);
311
        // TODO: DateTimeZones should be removed in BAP-8710. Tests should be passed for:
312
        //  - Oro\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest\CustomerControllerTest
313
        //  - Oro\Bundle\MagentoBundle\Tests\Functional\Controller\Api\Rest\MagentoCustomerControllerTest
314
        $customer->setCreatedAt(new \DateTime('now', new \DateTimezone('UTC')));
315
        $customer->setUpdatedAt(new \DateTime('now', new \DateTimezone('UTC')));
316
        if ($address) {
317
            $customer->addAddress($address);
318
        }
319
        $customer->setOwner($this->getUser());
320
        $customer->setOrganization($this->organization);
321
322
        $contact = $this->createContact($customer);
323
        if ($contactAddress) {
324
            $contact->addAddress($contactAddress);
325
        }
326
        $customer->setContact($contact);
327
        $this->em->persist($customer);
328
329
        return $customer;
330
    }
331
332
    /**
333
     * @param Customer $customer
334
     *
335
     * @return Contact
336
     */
337 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...
338
    {
339
        $contact = new Contact();
340
        $contact->setFirstName($customer->getFirstName());
341
        $contact->setLastName($customer->getLastName());
342
        $contact->setGender($customer->getGender());
343
        $contact->setOwner($this->getUser());
344
        $contact->setOrganization($this->organization);
345
        $this->em->persist($contact);
346
347
        return $contact;
348
    }
349
350
    /**
351
     * @return $this
352
     */
353
    protected function createWebSite()
354
    {
355
        $website = new Website();
356
        $website->setName('web site');
357
        $website->setOriginId(1);
358
        $website->setCode('web site code');
359
        $website->setChannel($this->integration);
360
        $this->setReference('website', $website);
361
        $this->em->persist($website);
362
        $this->website = $website;
363
364
        return $this;
365
    }
366
367
    /**
368
     * @return $this
369
     */
370
    protected function createStore()
371
    {
372
        $store = new Store;
373
        $store->setName('demo store');
374
        $store->setChannel($this->integration);
375
        $store->setCode(1);
376
        $store->setWebsite($this->website);
377
        $store->setOriginId(1);
378
        $this->em->persist($store);
379
        $this->store = $store;
380
        $this->setReference('store', $store);
381
382
        return $this;
383
    }
384
385
    /**
386
     * @return Account
387
     */
388 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...
389
    {
390
        $account = new Account;
391
        $account->setName('acc');
392
        $account->setOwner($this->getUser());
393
        $account->setOrganization($this->organization);
394
        $this->em->persist($account);
395
396
        return $account;
397
    }
398
399
    /**
400
     * @return $this
401
     */
402 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...
403
    {
404
        $customerGroup = new CustomerGroup;
405
        $customerGroup->setName('group');
406
        $customerGroup->setChannel($this->integration);
407
        $customerGroup->setOriginId(1);
408
        $this->em->persist($customerGroup);
409
        $this->setReference('customer_group', $customerGroup);
410
        $this->customerGroup = $customerGroup;
411
412
        return $this;
413
    }
414
415
    /**
416
     * @return $this
417
     */
418 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...
419
    {
420
        $customerGroup = new CustomerGroup;
421
        $customerGroup->setName('NOT LOGGED IN');
422
        $customerGroup->setChannel($this->integration);
423
        $customerGroup->setOriginId(0);
424
        $this->em->persist($customerGroup);
425
426
        return $this;
427
    }
428
429
    /**
430
     * @return User
431
     */
432 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...
433
    {
434
        if (!$this->user) {
435
            $this->user = $this->em->getRepository('OroUserBundle:User')->findOneBy(['username' => 'admin']);
436
        }
437
438
        return $this->user;
439
    }
440
441
    /**
442
     * @return LoadMagentoChannel
443
     */
444 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...
445
    {
446
        $channel = $this
447
            ->factory
448
            ->createBuilder()
449
            ->setName(self::CHANNEL_NAME)
450
            ->setChannelType(self::CHANNEL_TYPE)
451
            ->setStatus(Channel::STATUS_ACTIVE)
452
            ->setDataSource($this->integration)
453
            ->setOwner($this->organization)
454
            ->setEntities()
455
            ->getChannel();
456
        $this->em->persist($channel);
457
        $this->em->flush();
458
        $this->setReference('default_channel', $channel);
459
        $this->channel = $channel;
460
461
        return $this;
462
    }
463
}
464