Completed
Push — master ( a18731...e2e070 )
by
unknown
99:54 queued 47:45
created

B2bCustomerPhoneHandler::process()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 34
Code Lines 19

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
dl 34
loc 34
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 5
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Form\Handler;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
use Symfony\Component\Form\FormInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
10
11
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
12
use Oro\Bundle\SecurityBundle\SecurityFacade;
13
14
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer;
15
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomerPhone;
16
use OroCRM\Bundle\SalesBundle\Validator\B2bCustomerPhoneDeleteValidator;
17
18 View Code Duplication
class B2bCustomerPhoneHandler
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /** @var FormInterface */
21
    protected $form;
22
23
    /** @var Request */
24
    protected $request;
25
26
    /** @var EntityManagerInterface */
27
    protected $manager;
28
29
    /** @var  B2bCustomerPhoneDeleteValidator */
30
    protected $b2bCustomerPhoneDeleteValidator;
31
32
    /** @var SecurityFacade */
33
    protected $securityFacade;
34
35
    /**
36
     * @param FormInterface $form
37
     * @param Request $request
38
     * @param EntityManagerInterface $manager
39
     * @param B2bCustomerPhoneDeleteValidator $b2bCustomerPhoneDeleteValidator
40
     * @param SecurityFacade $securityFacade
41
     */
42
    public function __construct(
43
        FormInterface $form,
44
        Request $request,
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
45
        EntityManagerInterface $manager,
46
        B2bCustomerPhoneDeleteValidator $b2bCustomerPhoneDeleteValidator,
47
        SecurityFacade $securityFacade
48
    ) {
49
        $this->form    = $form;
50
        $this->request = $request;
51
        $this->manager = $manager;
52
        $this->b2bCustomerPhoneDeleteValidator = $b2bCustomerPhoneDeleteValidator;
53
        $this->securityFacade = $securityFacade;
54
    }
55
56
    /**
57
     * Process form
58
     *
59
     * @param B2bCustomerPhone $entity
60
     *
61
     * @return bool True on successful processing, false otherwise
62
     *
63
     * @throws AccessDeniedException
64
     */
65
    public function process(B2bCustomerPhone $entity)
66
    {
67
        $this->form->setData($entity);
68
69
        $submitData = [
70
            'phone' => $this->request->request->get('phone'),
71
            'primary' => $this->request->request->get('primary')
72
        ];
73
74
        if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
75
            $this->form->submit($submitData);
76
77
            $b2bCustomerId = $this->request->request->get('entityId');
78
            if ($this->form->isValid() && $b2bCustomerId) {
79
                $customer = $this->manager->find(
80
                    'OroCRMSalesBundle:B2bCustomer',
81
                    $b2bCustomerId
82
                );
83
                if (!$this->securityFacade->isGranted('EDIT', $customer)) {
84
                    throw new AccessDeniedException();
85
                }
86
87
                if ($customer->getPrimaryPhone() && $this->request->request->get('primary') === true) {
88
                    return false;
89
                }
90
91
                $this->onSuccess($entity, $customer);
92
93
                return true;
94
            }
95
        }
96
97
        return false;
98
    }
99
100
    /**
101
     * @param $id
102
     * @param ApiEntityManager $manager
103
     *
104
     * @throws \Exception
105
     */
106
    public function handleDelete($id, ApiEntityManager $manager)
107
    {
108
        /** @var B2bCustomerPhone $b2bCustomerPhone */
109
        $b2bCustomerPhone = $manager->find($id);
110
        if (!$this->securityFacade->isGranted('EDIT', $b2bCustomerPhone->getOwner())) {
111
            throw new AccessDeniedException();
112
        }
113
114
        if ($this->b2bCustomerPhoneDeleteValidator->validate($b2bCustomerPhone)) {
115
            $em = $manager->getObjectManager();
116
            $em->remove($b2bCustomerPhone);
117
            $em->flush();
118
        } else {
119
            throw new \Exception("orocrm.sales.phone.error.delete.more_one", 500);
120
        }
121
    }
122
123
    /**
124
     * @param B2bCustomerPhone $entity
125
     * @param B2bCustomer $customer
126
     */
127
    protected function onSuccess(B2bCustomerPhone $entity, B2bCustomer $customer)
128
    {
129
        $entity->setOwner($customer);
130
        $this->manager->persist($entity);
131
        $this->manager->flush();
132
    }
133
}
134