CustomerHandler::saveForm()   D
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 23
nc 15
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Oro\Bundle\MagentoBundle\Form\Handler;
4
5
use Symfony\Component\Form\FormInterface;
6
7
use Oro\Bundle\FormBundle\Model\UpdateHandler;
8
use Oro\Bundle\MagentoBundle\Entity\Address;
9
use Oro\Bundle\MagentoBundle\Entity\Customer;
10
use Oro\Bundle\MagentoBundle\Service\CustomerStateHandler;
11
12
class CustomerHandler extends UpdateHandler
13
{
14
    /**
15
     * @var CustomerStateHandler
16
     */
17
    protected $stateHandler;
18
19
    /**
20
     * @param CustomerStateHandler $stateHandler
21
     * @return CustomerHandler
22
     */
23
    public function setStateHandler($stateHandler)
24
    {
25
        $this->stateHandler = $stateHandler;
26
27
        return $this;
28
    }
29
30
    /**
31
     * @param Customer $entity
32
     * @return bool
33
     */
34
    public function handleRegister(Customer $entity)
35
    {
36
        if ($this->request->getMethod() === 'POST') {
37
            $manager = $this->doctrineHelper->getEntityManager($entity);
38
            $entity->setGuest(false);
39
            $entity->setIsActive(true);
40
            $this->stateHandler->markCustomerForSync($entity);
41
            $this->stateHandler->markAddressesForSync($entity);
42
43
            $manager->persist($entity);
44
            $manager->flush();
45
46
            return true;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function saveForm(FormInterface $form, $entity)
56
    {
57
        if (!$entity instanceof Customer) {
58
            throw new \InvalidArgumentException('Customer expected');
59
        }
60
61
        $form->setData($entity);
62
        if (in_array($this->request->getMethod(), ['POST', 'PUT'], true)) {
63
            $form->submit($this->request);
64
65
            if ($form->isValid()) {
66
                $addressesToSync = [];
67
                if ($entity->getId()) {
68
                    $this->stateHandler->markCustomerForSync($entity);
69
70
                    if (!$entity->getAddresses()->isEmpty()) {
71
                        foreach ($entity->getAddresses() as $address) {
72
                            if (!$address->getOriginId()) {
73
                                $addressesToSync[] = $address;
74
                            } else {
75
                                $this->stateHandler->markAddressForSync($address);
76
                            }
77
                        }
78
                    }
79
                }
80
                $this->saveEntity($entity);
81
82
                // Process trigger listen for update, because create will trigger export during import
83
                // This will schedule new entity for export
84
                if (!$entity->getOriginId()) {
85
                    $this->scheduleCustomerSyncToMagento($entity);
86
                }
87
88
                foreach ($addressesToSync as $address) {
89
                    $this->scheduleAddressSyncToMagento($address);
90
                }
91
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * @param object $entity
101
     */
102
    protected function saveEntity($entity)
103
    {
104
        $manager = $this->doctrineHelper->getEntityManager($entity);
105
        $manager->persist($entity);
106
107
        // flush entity with related entities
108
        $manager->flush();
109
    }
110
111
    /**
112
     * @param Customer $entity
113
     */
114
    protected function scheduleCustomerSyncToMagento(Customer $entity)
115
    {
116
        $this->stateHandler->markCustomerForSync($entity);
117
        $this->saveEntity($entity);
118
    }
119
120
    /**
121
     * @param Address $entity
122
     */
123
    protected function scheduleAddressSyncToMagento(Address $entity)
124
    {
125
        $this->stateHandler->markAddressForSync($entity);
126
        $this->saveEntity($entity);
127
    }
128
}
129