Completed
Push — master ( a18731...e2e070 )
by
unknown
99:54 queued 47:45
created

B2bCustomerEmailHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 10
dl 115
loc 115
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
C process() 34 34 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\SalesBundle\Form\Handler;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
7
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomer;
8
use OroCRM\Bundle\SalesBundle\Entity\B2bCustomerEmail;
9
use OroCRM\Bundle\SalesBundle\Validator\B2bCustomerEmailDeleteValidator;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13
14
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
15
use Oro\Bundle\SecurityBundle\SecurityFacade;
16
17 View Code Duplication
class B2bCustomerEmailHandler
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...
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,
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...
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