| 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\LeadEmail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use OroCRM\Bundle\SalesBundle\Entity\Lead; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class LeadEmailHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         FormFactory $form, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         Request $request, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 LeadEmail $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return bool True on successful processing, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @throws AccessDeniedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function process(LeadEmail $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $form = $this->form->create('orocrm_sales_lead_email', $entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $submitData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'email' => $this->request->request->get('email'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'primary' => $this->request->request->get('primary') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $form->submit($submitData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             if ($form->isValid() && $this->request->request->get('entityId')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 /** @var Lead $lead */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $lead = $this->manager->find( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     'OroCRMSalesBundle:Lead', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     $this->request->request->get('entityId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 if (!$this->securityFacade->isGranted('EDIT', $lead)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     throw new AccessDeniedException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 if ($lead->getPrimaryEmail() && $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 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 98 |  | View Code Duplication |     public function handleDelete($id, ApiEntityManager $manager) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         /** @var LeadEmail $leadEmail */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $leadEmail = $manager->find($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         if (!$this->securityFacade->isGranted('EDIT', $leadEmail->getOwner())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             throw new AccessDeniedException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         if ($leadEmail->isPrimary() && $leadEmail->getOwner()->getEmails()->count() === 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             $em = $manager->getObjectManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             $em->remove($leadEmail); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             $em->flush(); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             throw new \Exception("orocrm.lead.email.error.delete.more_one", 500); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param LeadEmail $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param Lead $lead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     protected function onSuccess(LeadEmail $entity, Lead $lead) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $entity->setOwner($lead); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->manager->persist($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->manager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
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.