Code Duplication    Length = 114-115 lines in 2 locations

src/OroCRM/Bundle/ContactBundle/Form/Handler/ContactEmailHandler.php 1 location

@@ 18-131 (lines=114) @@
15
use OroCRM\Bundle\ContactBundle\Entity\ContactEmail;
16
use OroCRM\Bundle\ContactBundle\Entity\Contact;
17
18
class ContactEmailHandler
19
{
20
    /** @var FormInterface */
21
    protected $form;
22
23
    /** @var Request */
24
    protected $request;
25
26
    /** @var EntityManagerInterface */
27
    protected $manager;
28
29
    /** @var  ContactEmailDeleteValidator */
30
    protected $contactEmailDeleteValidator;
31
32
    /** @var SecurityFacade */
33
    protected $securityFacade;
34
35
    /**
36
     * @param FormInterface $form
37
     * @param Request $request
38
     * @param EntityManagerInterface $manager
39
     * @param ContactEmailDeleteValidator $contactEmailDeleteValidator
40
     * @param SecurityFacade $securityFacade
41
     */
42
    public function __construct(
43
        FormInterface $form,
44
        Request $request,
45
        EntityManagerInterface $manager,
46
        ContactEmailDeleteValidator $contactEmailDeleteValidator,
47
        SecurityFacade $securityFacade
48
    ) {
49
        $this->form    = $form;
50
        $this->request = $request;
51
        $this->manager = $manager;
52
        $this->contactEmailDeleteValidator = $contactEmailDeleteValidator;
53
        $this->securityFacade = $securityFacade;
54
    }
55
56
    /**
57
     * Process form
58
     *
59
     * @param ContactEmail $entity
60
     *
61
     * @return bool True on successful processing, false otherwise
62
     *
63
     * @throws AccessDeniedException
64
     */
65
    public function process(ContactEmail $entity)
66
    {
67
        $this->form->setData($entity);
68
69
        $submitData = [
70
            'email' => $this->request->request->get('email'),
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
            if ($this->form->isValid() && $this->request->request->get('contactId')) {
78
                $contact = $this->manager->find(
79
                    'OroCRMContactBundle:Contact',
80
                    $this->request->request->get('contactId')
81
                );
82
                if (!$this->securityFacade->isGranted('EDIT', $contact)) {
83
                    throw new AccessDeniedException();
84
                }
85
86
                if ($contact->getPrimaryEmail() && $this->request->request->get('primary') === true) {
87
                    return false;
88
                }
89
90
                $this->onSuccess($entity, $contact);
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 ContactEmail $contactEmail */
107
        $contactEmail = $manager->find($id);
108
        if (!$this->securityFacade->isGranted('EDIT', $contactEmail->getOwner())) {
109
            throw new AccessDeniedException();
110
        }
111
112
        if ($this->contactEmailDeleteValidator->validate($contactEmail)) {
113
            $em = $manager->getObjectManager();
114
            $em->remove($contactEmail);
115
            $em->flush();
116
        } else {
117
            throw new \Exception("oro.contact.email.error.delete.more_one", 500);
118
        }
119
    }
120
121
    /**
122
     * @param ContactEmail $entity
123
     * @param Contact $contact
124
     */
125
    protected function onSuccess(ContactEmail $entity, Contact $contact)
126
    {
127
        $entity->setOwner($contact);
128
        $this->manager->persist($entity);
129
        $this->manager->flush();
130
    }
131
}
132

src/OroCRM/Bundle/ContactBundle/Form/Handler/ContactPhoneHandler.php 1 location

@@ 18-132 (lines=115) @@
15
use OroCRM\Bundle\ContactBundle\Entity\ContactPhone;
16
use OroCRM\Bundle\ContactBundle\Entity\Contact;
17
18
class ContactPhoneHandler
19
{
20
    /** @var FormInterface */
21
    protected $form;
22
23
    /** @var Request */
24
    protected $request;
25
26
    /** @var EntityManagerInterface */
27
    protected $manager;
28
29
    /** @var  ContactPhoneDeleteValidator */
30
    protected $contactPhoneDeleteValidator;
31
32
    /** @var SecurityFacade */
33
    protected $securityFacade;
34
35
    /**
36
     * @param FormInterface $form
37
     * @param Request $request
38
     * @param EntityManagerInterface $manager
39
     * @param ContactPhoneDeleteValidator $contactPhoneDeleteValidator
40
     * @param SecurityFacade $securityFacade
41
     */
42
    public function __construct(
43
        FormInterface $form,
44
        Request $request,
45
        EntityManagerInterface $manager,
46
        ContactPhoneDeleteValidator $contactPhoneDeleteValidator,
47
        SecurityFacade $securityFacade
48
    ) {
49
        $this->form    = $form;
50
        $this->request = $request;
51
        $this->manager = $manager;
52
        $this->contactPhoneDeleteValidator = $contactPhoneDeleteValidator;
53
        $this->securityFacade = $securityFacade;
54
    }
55
56
    /**
57
     * Process form
58
     *
59
     * @param ContactPhone $entity
60
     *
61
     * @return bool True on successful processing, false otherwise
62
     *
63
     * @throws AccessDeniedException
64
     */
65
    public function process(ContactPhone $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
            if ($this->form->isValid() && $this->request->request->get('contactId')) {
78
                $contact = $this->manager->find(
79
                    'OroCRMContactBundle:Contact',
80
                    $this->request->request->get('contactId')
81
                );
82
                if (!$this->securityFacade->isGranted('EDIT', $contact)) {
83
                    throw new AccessDeniedException();
84
                }
85
86
                if ($contact->getPrimaryPhone() && $this->request->request->get('primary') === true) {
87
                    return false;
88
                }
89
90
                $this->onSuccess($entity, $contact);
91
92
                return true;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * @param $id
101
     * @param ApiEntityManager $manager
102
     *
103
     * @throws \Exception
104
     */
105
    public function handleDelete($id, ApiEntityManager $manager)
106
    {
107
        /** @var ContactPhone $contactPhone */
108
        $contactPhone = $manager->find($id);
109
        if (!$this->securityFacade->isGranted('EDIT', $contactPhone->getOwner())) {
110
            throw new AccessDeniedException();
111
        }
112
113
        if ($this->contactPhoneDeleteValidator->validate($contactPhone)) {
114
            $em = $manager->getObjectManager();
115
            $em->remove($contactPhone);
116
            $em->flush();
117
        } else {
118
            throw new \Exception("oro.contact.phone.error.delete.more_one", 500);
119
        }
120
    }
121
122
    /**
123
     * @param ContactPhone $entity
124
     * @param Contact $contact
125
     */
126
    protected function onSuccess(ContactPhone $entity, Contact $contact)
127
    {
128
        $entity->setOwner($contact);
129
        $this->manager->persist($entity);
130
        $this->manager->flush();
131
    }
132
}
133