Completed
Push — master ( 0b205b...bde6a0 )
by
unknown
10:16
created

CustomerStrategy::afterProcessEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Strategy;
4
5
use Oro\Bundle\AddressBundle\Entity\Region;
6
7
use OroCRM\Bundle\MagentoBundle\Entity\Address;
8
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
9
use OroCRM\Bundle\MagentoBundle\Provider\Reader\ContextCustomerReader;
10
11
class CustomerStrategy extends AbstractImportStrategy
12
{
13
    /**
14
     * @var Address[]
15
     */
16
    protected $importingAddresses = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $addressRegions = [];
22
23
    /**
24
     * @param Customer $entity
25
     * @return Customer
26
     */
27
    protected function beforeProcessEntity($entity)
28
    {
29
        $this->importingAddresses = [];
30
        $this->addressRegions = [];
31
        $importingAddresses = $entity->getAddresses();
32
        if ($importingAddresses) {
33
            foreach ($importingAddresses as $address) {
34
                if ($address->getSyncState() !== Address::SYNC_TO_MAGENTO) {
35
                    $originId = $address->getOriginId();
36
                    $this->importingAddresses[$originId] = $address;
37
38
                    if ($address->getRegion()) {
39
                        $this->addressRegions[$originId] = $address->getRegion()->getCombinedCode();
40
                    } else {
41
                        $this->addressRegions[$originId] = null;
42
                    }
43
                }
44
            }
45
        }
46
47
        return parent::beforeProcessEntity($entity);
48
    }
49
50
    /**
51
     * @param Customer $entity
52
     * @return Customer
53
     */
54
    protected function afterProcessEntity($entity)
55
    {
56
        $this->processAddresses($entity);
57
58
        $now = new \DateTime('now', new \DateTimeZone('UTC'));
59
        if (!$entity->getImportedAt()) {
60
            $entity->setImportedAt($now);
61
        }
62
        $entity->setSyncedAt($now);
63
64
        $this->appendDataToContext(ContextCustomerReader::CONTEXT_POST_PROCESS_CUSTOMERS, $entity->getOriginId());
65
66
        return parent::afterProcessEntity($entity);
67
    }
68
69
    /**
70
     * @param Customer $entity
71
     */
72
    protected function processAddresses(Customer $entity)
73
    {
74
        if (!$entity->getAddresses()->isEmpty()) {
75
            /** @var Address $address */
76
            foreach ($entity->getAddresses() as $address) {
77
                if ($address->getSyncState() !== Address::SYNC_TO_MAGENTO) {
78
                    $originId = $address->getOriginId();
79
                    if (array_key_exists($originId, $this->importingAddresses)) {
80
                        $remoteAddress = $this->importingAddresses[$originId];
81
                        $this->addressHelper->mergeAddressTypes($address, $remoteAddress);
82
83
                        if (!empty($this->addressRegions[$originId]) && $address->getCountry()) {
84
                            $this->addressHelper->updateRegionByMagentoRegionId(
85
                                $address,
86
                                $address->getCountry()->getIso2Code(),
87
                                $this->addressRegions[$originId]
88
                            );
89
                        }
90
                    }
91
                }
92
                $address->setOwner($entity);
93
            }
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function findExistingEntity($entity, array $searchContext = [])
101
    {
102
        $existingEntity = null;
0 ignored issues
show
Unused Code introduced by
$existingEntity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
103
104
        if ($entity instanceof Customer) {
105
            $website = $this->databaseHelper->findOneBy(
106
                'OroCRM\Bundle\MagentoBundle\Entity\Website',
107
                [
108
                    'originId' => $entity->getWebsite()->getOriginId(),
109
                    'channel' => $entity->getChannel()
110
                ]
111
            );
112
113
            if ($website) {
114
                $searchContext['website'] = $website;
115
            }
116
            /** @var Customer $existingEntity */
117
            $existingEntity = parent::findExistingEntity($entity, $searchContext);
118
119
            if (!$existingEntity) {
120
                $existingEntity = $this->databaseHelper->findOneBy(
121
                    'OroCRM\Bundle\MagentoBundle\Entity\Customer',
122
                    [
123
                        'email' => $entity->getEmail(),
124
                        'channel' => $entity->getChannel(),
125
                        'website' => $website
126
                    ]
127
                );
128
                if ($existingEntity && $existingEntity->getId()) {
129
                    if ($existingEntity->isGuest()) {
130
                        $existingEntity->setGuest(false);
131
                        $existingEntity->setIsActive(true);
132
                    }
133
                    if ($entity->getOriginId()) {
134
                        $existingEntity->setOriginId($entity->getOriginId());
135
                    }
136
                }
137
            }
138
        } elseif ($entity instanceof Region) {
139
            $existingEntity = $this->findRegionEntity($entity);
140
141
        } else {
142
            /** @var Customer $existingEntity */
143
            $existingEntity = parent::findExistingEntity($entity, $searchContext);
144
        }
145
146
        return $existingEntity;
147
    }
148
149
    /**
150
     * @param Region $entity
151
     *
152
     * @return null|object
153
     */
154 View Code Duplication
    protected function findRegionEntity($entity)
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...
155
    {
156
        $existingEntity = null;
157
158
        /** @var \OroCRM\Bundle\MagentoBundle\Entity\Region $magentoRegion */
159
        $magentoRegion = $this->databaseHelper->findOneBy(
160
            'OroCRM\Bundle\MagentoBundle\Entity\Region',
161
            [
162
                'regionId' => $entity->getCombinedCode()
163
            ]
164
        );
165
        if ($magentoRegion) {
166
            $existingEntity = $this->databaseHelper->findOneBy(
167
                'Oro\Bundle\AddressBundle\Entity\Region',
168
                [
169
                    'combinedCode' => $magentoRegion->getCombinedCode()
170
                ]
171
            );
172
        }
173
174
        return $existingEntity;
175
    }
176
}
177