Completed
Push — 1.10 ( bee206...263818 )
by
unknown
08:47
created

ContactAddOrReplaceStrategy::afterProcessEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\ImportExport\Strategy;
4
5
use Doctrine\Common\Util\ClassUtils;
6
7
use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy;
8
use OroCRM\Bundle\ContactBundle\Entity\Contact;
9
10
class ContactAddOrReplaceStrategy extends ConfigurableAddOrReplaceStrategy
11
{
12
    /**
13
     * @var ContactImportHelper
14
     */
15
    protected $contactImportHelper;
16
17
    /**
18
     * @param ContactImportHelper $contactImportHelper
19
     */
20
    public function setContactImportHelper(ContactImportHelper $contactImportHelper)
21
    {
22
        $this->contactImportHelper = $contactImportHelper;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 View Code Duplication
    protected function importExistingEntity(
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...
29
        $entity,
30
        $existingEntity,
31
        $itemData = null,
32
        array $excludedFields = array()
33
    ) {
34
        // manually handle recursive relation to accounts
35
        $entityName = ClassUtils::getClass($entity);
36
        $fieldName = 'accounts';
37
38
        if ($entity instanceof Contact
39
            && $existingEntity instanceof Contact
40
            && !$this->isFieldExcluded($entityName, $fieldName, $itemData)
41
            && !in_array($fieldName, $excludedFields)
42
        ) {
43
            foreach ($existingEntity->getAccounts() as $account) {
44
                $existingEntity->removeAccount($account);
45
            }
46
47
            foreach ($entity->getAccounts() as $account) {
48
                $account->removeContact($entity);
49
                $existingEntity->addAccount($account);
50
            }
51
52
            $excludedFields[] = $fieldName;
53
        }
54
55
        parent::importExistingEntity($entity, $existingEntity, $itemData, $excludedFields);
56
57
        if ($existingEntity instanceof Contact) {
58
            $this->fixDuplicateEntities($existingEntity);
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function beforeProcessEntity($entity)
66
    {
67
        /** @var Contact $entity */
68
        $entity = parent::beforeProcessEntity($entity);
69
70
        // need to manually set empty types to skip merge from existing entities
71
        $itemData = $this->context->getValue('itemData');
72
73
        if (!empty($itemData['addresses'])) {
74
            foreach ($itemData['addresses'] as $key => $address) {
75
                if (!isset($address['types'])) {
76
                    $itemData['addresses'][$key]['types'] = array();
77
                }
78
            }
79
80
            $this->context->setValue('itemData', $itemData);
81
        }
82
83
        return $entity;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    protected function afterProcessEntity($entity)
90
    {
91
        /** @var Contact $entity */
92
        $entity = parent::afterProcessEntity($entity);
93
94
        $this->contactImportHelper->updateScalars($entity);
95
        $this->contactImportHelper->updatePrimaryEntities($entity);
96
97
        return $entity;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function findEntityByIdentityValues($entityName, array $identityValues)
104
    {
105
        // contact last name and first name must be in this order to support compound index
106
        if ($entityName == 'OroCRM\Bundle\ContactBundle\Entity\Contact') {
107
            if (array_key_exists('firstName', $identityValues) && array_key_exists('lastName', $identityValues)) {
108
                $firstName = $identityValues['firstName'];
109
                $lastName = $identityValues['lastName'];
110
                unset($identityValues['firstName']);
111
                unset($identityValues['lastName']);
112
                $identityValues = array_merge(
113
                    array('lastName' => $lastName, 'firstName' => $firstName),
114
                    $identityValues
115
                );
116
            }
117
        }
118
119
        return parent::findEntityByIdentityValues($entityName, $identityValues);
120
    }
121
122
    /**
123
     * @param Contact $existingEntity
124
     */
125
    private function fixDuplicateEntities(Contact $existingEntity)
126
    {
127
        // need to remove duplicated entities, because Account and Contact add* related methods calls each other
128
        // eg. Contact::addAccount also calls Account::addContact
129
        foreach ($existingEntity->getAccounts() as $account) {
130
            foreach ($account->getContacts() as $contact) {
131
                $contactExistCallback = function ($key, Contact $value) use ($contact) {
132
                    return $contact !== $value && (int)$contact->getId() === (int)$value->getId();
133
                };
134
                if ($contact->getId() !== null && $account->getContacts()->exists($contactExistCallback)) {
135
                    $account->removeContact($contact);
136
                }
137
            }
138
        }
139
    }
140
}
141