Completed
Push — master ( 535b09...0667cb )
by
unknown
56:23
created

LeadPhoneHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 24.55 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 10
dl 27
loc 110
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
C process() 0 34 7
A handleDelete() 16 16 4
A onSuccess() 0 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\SalesBundle\Form\Handler;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
use Symfony\Component\Form\FormFactory;
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\Lead;
15
use OroCRM\Bundle\SalesBundle\Entity\LeadPhone;
16
17
class LeadPhoneHandler
18
{
19
    /** @var FormFactory */
20
    protected $form;
21
22
    /** @var Request */
23
    protected $request;
24
25
    /** @var EntityManagerInterface */
26
    protected $manager;
27
28
    /** @var SecurityFacade */
29
    protected $securityFacade;
30
31
    /**
32
     * @param FormFactory $form
33
     * @param Request $request
34
     * @param EntityManagerInterface $manager
35
     * @param SecurityFacade $securityFacade
36
     */
37 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
38
        FormFactory $form,
39
        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...
40
        EntityManagerInterface $manager,
41
        SecurityFacade $securityFacade
42
    ) {
43
        $this->form    = $form;
44
        $this->request = $request;
45
        $this->manager = $manager;
46
        $this->securityFacade = $securityFacade;
47
    }
48
49
    /**
50
     * Process form
51
     *
52
     * @param LeadPhone $entity
53
     *
54
     * @return bool True on successful processing, false otherwise
55
     *
56
     * @throws AccessDeniedException
57
     */
58
    public function process(LeadPhone $entity)
59
    {
60
        $form = $this->form->create('orocrm_sales_lead_phone', $entity);
61
62
        $submitData = [
63
            'phone' => $this->request->request->get('phone'),
64
            'primary' => $this->request->request->get('primary')
65
        ];
66
67
        if (in_array($this->request->getMethod(), ['POST', 'PUT'])) {
68
            $form->submit($submitData);
69
70
            $leadId = $this->request->request->get('entityId');
71
            if ($form->isValid() && $leadId) {
72
                $lead = $this->manager->find(
73
                    'OroCRMSalesBundle:Lead',
74
                    $leadId
75
                );
76
                if (!$this->securityFacade->isGranted('EDIT', $lead)) {
77
                    throw new AccessDeniedException();
78
                }
79
80
                if ($lead->getPrimaryPhone() && $this->request->request->get('primary') === true) {
81
                    return false;
82
                }
83
84
                $this->onSuccess($entity, $lead);
85
86
                return true;
87
            }
88
        }
89
90
        return false;
91
    }
92
93
    /**
94
     * @param $id
95
     * @param ApiEntityManager $manager
96
     *
97
     * @throws \Exception
98
     */
99 View Code Duplication
    public function handleDelete($id, ApiEntityManager $manager)
0 ignored issues
show
Duplication introduced by
This method 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...
100
    {
101
        /** @var LeadPhone $leadPhone */
102
        $leadPhone = $manager->find($id);
103
        if (!$this->securityFacade->isGranted('EDIT', $leadPhone->getOwner())) {
104
            throw new AccessDeniedException();
105
        }
106
107
        if ($leadPhone->isPrimary() && $leadPhone->getOwner()->getPhones()->count() === 1) {
108
            $em = $manager->getObjectManager();
109
            $em->remove($leadPhone);
110
            $em->flush();
111
        } else {
112
            throw new \Exception("orocrm.lead.phone.error.delete.more_one", 500);
113
        }
114
    }
115
116
    /**
117
     * @param LeadPhone $entity
118
     * @param Lead $lead
119
     */
120
    protected function onSuccess(LeadPhone $entity, Lead $lead)
121
    {
122
        $entity->setOwner($lead);
123
        $this->manager->persist($entity);
124
        $this->manager->flush();
125
    }
126
}
127