Completed
Push — 1.10 ( a517a7...e5bcfb )
by
unknown
20:21
created

beforeProcessEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\ImportExport\Strategy;
4
5
use Oro\Bundle\AddressBundle\Entity\Address;
6
use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy;
7
8
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer;
9
10
class B2bConfigurableAddOrReplaceStrategy extends ConfigurableAddOrReplaceStrategy
11
{
12
    /**
13
     * Save state about exists Billing Address value in entity or not.
14
     * Value is updated in function beforeProcessEntity and used
15
     * in function afterProcessEntity
16
     *
17
     * @var bool
18
     */
19
    protected $isBillingAddress = true;
20
21
    /**
22
     * Save state about exists Shipping Address value in entity or not.
23
     * Value is updated in function beforeProcessEntity and used
24
     * in function afterProcessEntity
25
     *
26
     * @var bool
27
     */
28
    protected $isShippingAddress = true;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function beforeProcessEntity($entity)
34
    {
35
        /** @var B2bCustomer $entity */
36
        $entity = parent::beforeProcessEntity($entity);
37
        $this->checkEmptyAddresses($entity);
38
39
        return $entity;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function afterProcessEntity($entity)
46
    {
47
        /** @var B2bCustomer $entity */
48
        $entity = parent::afterProcessEntity($entity);
49
        $this->clearEmptyAddresses($entity);
50
51
        $this->guessRegion($entity->getBillingAddress());
52
        $this->guessRegion($entity->getShippingAddress());
53
54
        return $entity;
55
    }
56
57
    /**
58
     * @param B2bCustomer $entity
59
     */
60
    protected function checkEmptyAddresses(B2bCustomer $entity)
61
    {
62
        if (!$entity->getBillingAddress()) {
63
            $this->isBillingAddress = false;
64
        }
65
66
        if (!$entity->getShippingAddress()) {
67
            $this->isShippingAddress = false;
68
        }
69
    }
70
71
    /**
72
     * @param B2bCustomer $entity
73
     */
74
    protected function clearEmptyAddresses(B2bCustomer $entity)
75
    {
76
        if (!$this->isBillingAddress) {
77
            $entity->setBillingAddress(null);
78
        }
79
80
        if (!$this->isShippingAddress) {
81
            $entity->setShippingAddress(null);
82
        }
83
    }
84
85
    /**
86
     * @param Address $address
87
     */
88
    protected function guessRegion($address)
89
    {
90
        if ($address
91
            && $address->getCountry() && $address->getRegionText()
92
            && !$address->getRegion()
93
        ) {
94
            $region = $this->doctrineHelper
95
                ->getEntityRepository('OroAddressBundle:Region')
96
                ->findOneBy(
97
                    [
98
                        'country' => $address->getCountry(),
99
                        'name'    => $address->getRegionText()
100
                    ]
101
                );
102
            if ($region) {
103
                $address->setRegion($region);
104
                $address->setRegionText(null);
105
            }
106
        }
107
    }
108
}
109