Completed
Push — 1.9 ( 5c3e2e...1529fd )
by
unknown
60:20
created

LoadOrderData::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 1
f 0
cc 2
eloc 18
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\AnalyticsBundle\Tests\Functional\DataFixtures;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\DataFixtures\AbstractFixture;
7
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
12
use Oro\Bundle\UserBundle\Entity\User;
13
use Oro\Bundle\UserBundle\Migrations\Data\ORM\LoadAdminUserData;
14
use OroCRM\Bundle\MagentoBundle\Entity\Order;
15
16
class LoadOrderData extends AbstractFixture implements DependentFixtureInterface, ContainerAwareInterface
17
{
18
    /**
19
     * @var array Orders
20
     */
21
    protected $orderData = [
22
        [
23
            'status' => 'canceled',
24
            'incrementId' => '1',
25
            'customerEmail' => '[email protected]',
26
            'createdSub' => 'P1D',
27
            'updatedSub' => 'P1D',
28
            'discountAmount' => 4.40,
29
            'taxAmount' => 12.47,
30
            'shippingAmount' => 5,
31
            'totalPaidAmount' => 17.85,
32
            'subtotalAmount' => 17.85,
33
            'totalInvoicedAmount' => 11,
34
            'totalRefundedAmount' => 4,
35
            'totalCanceledAmount' => 0,
36
            'shippingMethod' => 'some unique shipping method',
37
            'remoteIp' => 'unique ip',
38
            'giftMessage' => 'some very unique gift message',
39
            'dataChannel' => 'Channel.CustomerChannel',
40
            'customer' => 'Channel.CustomerIdentity.CustomerIdentity',
41
42
            'reference' => 'order_1'
43
        ],
44
        [
45
            'status' => 'done',
46
            'incrementId' => '2',
47
            'customerEmail' => '[email protected]',
48
            'createdSub' => 'P6D',
49
            'updatedSub' => 'P6D',
50
            'discountAmount' => 4.40,
51
            'taxAmount' => 12.47,
52
            'shippingAmount' => 5,
53
            'totalPaidAmount' => 17.85,
54
            'subtotalAmount' => 15.5,
55
            'totalInvoicedAmount' => 11,
56
            'totalRefundedAmount' => 4,
57
            'totalCanceledAmount' => 0,
58
            'shippingMethod' => 'some unique shipping method',
59
            'remoteIp' => 'unique ip',
60
            'giftMessage' => 'some very unique gift message',
61
            'dataChannel' => 'Channel.CustomerChannel',
62
            'customer' => 'Channel.CustomerIdentity.CustomerIdentity',
63
64
            'reference' => 'order_2'
65
        ],
66
        [
67
            'status' => 'done',
68
            'incrementId' => '3',
69
            'customerEmail' => '[email protected]',
70
            'createdSub' => 'P1D',
71
            'updatedSub' => 'P1D',
72
            'discountAmount' => 4.40,
73
            'taxAmount' => 12.47,
74
            'shippingAmount' => 5,
75
            'totalPaidAmount' => 6.85,
76
            'subtotalAmount' => 15.5,
77
            'totalInvoicedAmount' => 11,
78
            'totalRefundedAmount' => 4,
79
            'totalCanceledAmount' => 0,
80
            'shippingMethod' => 'some unique shipping method',
81
            'remoteIp' => 'unique ip',
82
            'giftMessage' => 'some very unique gift message',
83
            'dataChannel' => 'Channel.CustomerChannel',
84
            'customer' => 'Channel.CustomerChannel.Customer',
85
86
            'reference' => 'order_3'
87
        ],
88
        [
89
            'status' => 'done',
90
            'incrementId' => '4',
91
            'customerEmail' => '[email protected]',
92
            'createdSub' => 'P364D',
93
            'updatedSub' => 'P364D',
94
            'discountAmount' => 4.40,
95
            'taxAmount' => 12.47,
96
            'shippingAmount' => 5,
97
            'totalPaidAmount' => 17.85,
98
            'subtotalAmount' => 5.85,
99
            'totalInvoicedAmount' => 11,
100
            'totalRefundedAmount' => 4,
101
            'totalCanceledAmount' => 0,
102
            'shippingMethod' => 'some unique shipping method',
103
            'remoteIp' => 'unique ip',
104
            'giftMessage' => 'some very unique gift message',
105
            'dataChannel' => 'Channel.CustomerChannel',
106
            'customer' => 'Channel.CustomerChannel.Customer',
107
108
            'reference' => 'order_4'
109
        ],
110
    ];
111
112
    /**
113
     * @var ContainerInterface
114
     */
115
    protected $container;
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function setContainer(ContainerInterface $container = null)
121
    {
122
        $this->container = $container;
123
    }
124
125
    /**
126
     * @param object $entity
127
     * @param array $data
128
     * @param array $excludeProperties
129
     */
130 View Code Duplication
    public function setEntityPropertyValues($entity, array $data, array $excludeProperties = [])
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...
131
    {
132
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
133
        foreach ($data as $property => $value) {
134
            if (in_array($property, $excludeProperties)) {
135
                continue;
136
            }
137
            $propertyAccessor->setValue($entity, $property, $value);
138
        }
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function load(ObjectManager $manager)
145
    {
146
        $userManager = $this->container->get('oro_user.manager');
147
        /** @var User $admin */
148
        $admin = $userManager->findUserByEmail(LoadAdminUserData::DEFAULT_ADMIN_EMAIL);
149
        $organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
150
151
        foreach ($this->orderData as $data) {
152
            $entity = new Order();
153
            $entity->setOwner($admin);
154
            $entity->setOrganization($organization);
155
            $created = new \DateTime('now', new \DateTimeZone('UTC'));
156
            $entity->setCreatedAt($created->sub(new \DateInterval($data['createdSub'])));
157
            $updated = new \DateTime('now', new \DateTimeZone('UTC'));
158
            $entity->setUpdatedAt($updated->sub(new \DateInterval($data['updatedSub'])));
159
160
            $data['dataChannel'] = $this->getReference($data['dataChannel']);
161
            $data['customer'] = $this->getReference($data['customer']);
162
163
            $this->setEntityPropertyValues($entity, $data, ['reference', 'createdSub', 'updatedSub']);
164
            $this->setReference($data['reference'], $entity);
165
            $manager->persist($entity);
166
        }
167
        $manager->flush();
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getDependencies()
174
    {
175
        return [__NAMESPACE__ . '\LoadEntitiesData'];
176
    }
177
}
178