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

ContactEmailHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 8
Bugs 4 Features 3
Metric Value
wmc 12
c 8
b 4
f 3
lcom 1
cbo 10
dl 114
loc 114
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
C process() 33 33 7
A handleDelete() 16 16 3
A onSuccess() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ContactEmailDeleteValidator;
15
use OroCRM\Bundle\ContactBundle\Entity\ContactEmail;
16
use OroCRM\Bundle\ContactBundle\Entity\Contact;
17
18 View Code Duplication
class ContactEmailHandler
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  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,
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
        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