Completed
Push — master ( 3560db...6e658b )
by
unknown
08:14
created

ContactPhoneHandler::process()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 33
Code Lines 18

Duplication

Lines 33
Ratio 100 %

Importance

Changes 5
Bugs 3 Features 2
Metric Value
c 5
b 3
f 2
dl 33
loc 33
rs 6.7273
cc 7
eloc 18
nc 5
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\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\ContactBundle\Validator\ContactPhoneDeleteValidator;
15
use OroCRM\Bundle\ContactBundle\Entity\ContactPhone;
16
use OroCRM\Bundle\ContactBundle\Entity\Contact;
17
18 View Code Duplication
class ContactPhoneHandler
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  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,
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
        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