Code Duplication    Length = 115-116 lines in 2 locations

src/OroCRM/Bundle/SalesBundle/Form/Handler/B2bCustomerEmailHandler.php 1 location

@@ 17-131 (lines=115) @@
14
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
15
use Oro\Bundle\SecurityBundle\SecurityFacade;
16
17
class B2bCustomerEmailHandler
18
{
19
    /** @var FormInterface */
20
    protected $form;
21
22
    /** @var Request */
23
    protected $request;
24
25
    /** @var EntityManagerInterface */
26
    protected $manager;
27
28
    /** @var  B2bCustomerEmailDeleteValidator */
29
    protected $b2bCustomerEmailDeleteValidator;
30
31
    /** @var SecurityFacade */
32
    protected $securityFacade;
33
34
    /**
35
     * @param FormInterface $form
36
     * @param Request $request
37
     * @param EntityManagerInterface $manager
38
     * @param B2bCustomerEmailDeleteValidator $b2bCustomerEmailDeleteValidator
39
     * @param SecurityFacade $securityFacade
40
     */
41
    public function __construct(
42
        FormInterface $form,
43
        Request $request,
44
        EntityManagerInterface $manager,
45
        B2bCustomerEmailDeleteValidator $b2bCustomerEmailDeleteValidator,
46
        SecurityFacade $securityFacade
47
    ) {
48
        $this->form    = $form;
49
        $this->request = $request;
50
        $this->manager = $manager;
51
        $this->b2bCustomerEmailDeleteValidator = $b2bCustomerEmailDeleteValidator;
52
        $this->securityFacade = $securityFacade;
53
    }
54
55
    /**
56
     * Process form
57
     *
58
     * @param B2bCustomerEmail $entity
59
     *
60
     * @return bool True on successful processing, false otherwise
61
     *
62
     * @throws AccessDeniedException
63
     */
64
    public function process(B2bCustomerEmail $entity)
65
    {
66
        $this->form->setData($entity);
67
68
        $submitData = [
69
            'email' => $this->request->request->get('email'),
70
            'primary' => $this->request->request->get('primary')
71
        ];
72
73
        if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
74
            $this->form->submit($submitData);
75
76
            $b2bCustomerId = $this->request->request->get('entityId');
77
            if ($this->form->isValid() && $b2bCustomerId) {
78
                $customer = $this->manager->find(
79
                    'OroCRMSalesBundle:B2bCustomer',
80
                    $b2bCustomerId
81
                );
82
                if (!$this->securityFacade->isGranted('EDIT', $customer)) {
83
                    throw new AccessDeniedException();
84
                }
85
86
                if ($customer->getPrimaryEmail() && $this->request->request->get('primary') === true) {
87
                    return false;
88
                }
89
90
                $this->onSuccess($entity, $customer);
91
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * @param $id
101
     * @param ApiEntityManager $manager
102
     * @throws \Exception
103
     */
104
    public function handleDelete($id, ApiEntityManager $manager)
105
    {
106
        /** @var B2bCustomerEmail $customerEmail */
107
        $customerEmail = $manager->find($id);
108
        if (!$this->securityFacade->isGranted('EDIT', $customerEmail->getOwner())) {
109
            throw new AccessDeniedException();
110
        }
111
112
        if ($this->b2bCustomerEmailDeleteValidator->validate($customerEmail)) {
113
            $em = $manager->getObjectManager();
114
            $em->remove($customerEmail);
115
            $em->flush();
116
        } else {
117
            throw new \Exception("orocrm.sales.email.error.delete.more_one", 500);
118
        }
119
    }
120
121
    /**
122
     * @param B2bCustomerEmail $entity
123
     * @param B2bCustomer $customer
124
     */
125
    protected function onSuccess(B2bCustomerEmail $entity, B2bCustomer $customer)
126
    {
127
        $entity->setOwner($customer);
128
        $this->manager->persist($entity);
129
        $this->manager->flush();
130
    }
131
}
132

src/OroCRM/Bundle/SalesBundle/Form/Handler/B2bCustomerPhoneHandler.php 1 location

@@ 18-133 (lines=116) @@
15
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomerPhone;
16
use OroCRM\Bundle\SalesBundle\Validator\B2bCustomerPhoneDeleteValidator;
17
18
class B2bCustomerPhoneHandler
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,
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