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

LeadAddressController::update()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 34
Code Lines 20

Duplication

Lines 34
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 34
loc 34
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 20
nc 15
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
11
12
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
13
use OroCRM\Bundle\SalesBundle\Entity\LeadAddress;
14
use OroCRM\Bundle\SalesBundle\Entity\Lead;
15
16
/**
17
 * @Route("/lead")
18
 */
19 View Code Duplication
class LeadAddressController extends Controller
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...
20
{
21
    /**
22
     * @Route("/address-book/{id}", name="orocrm_sales_lead_address_book", requirements={"id"="\d+"})
23
     * @Template
24
     * @AclAncestor("orocrm_sales_lead_view")
25
     */
26
    public function addressBookAction(Lead $lead)
27
    {
28
        return array(
29
            'entity' => $lead,
30
            'address_edit_acl_resource' => 'orocrm_sales_lead_update'
31
        );
32
    }
33
34
    /**
35
     * @Route(
36
     *      "/{leadId}/address-create",
37
     *      name="orocrm_sales_lead_address_create",
38
     *      requirements={"leadId"="\d+"}
39
     * )
40
     * @Template("OroCRMSalesBundle:LeadAddress:update.html.twig")
41
     * @AclAncestor("orocrm_sales_lead_update")
42
     * @ParamConverter("lead", options={"id" = "leadId"})
43
     */
44
    public function createAction(Lead $lead)
45
    {
46
        return $this->update($lead, new LeadAddress());
47
    }
48
49
    /**
50
     * @Route(
51
     *      "/{leadId}/address-update/{id}",
52
     *      name="orocrm_sales_lead_address_update",
53
     *      requirements={"leadId"="\d+","id"="\d+"},defaults={"id"=0}
54
     * )
55
     * @Template
56
     * @AclAncestor("orocrm_sales_lead_update")
57
     * @ParamConverter("lead", options={"id" = "leadId"})
58
     */
59
    public function updateAction(Lead $lead, LeadAddress $address)
60
    {
61
        return $this->update($lead, $address);
62
    }
63
64
    /**
65
     * @param Lead $lead
66
     * @param LeadAddress $address
67
     * @return array
68
     * @throws BadRequestHttpException
69
     */
70
    protected function update(Lead $lead, LeadAddress $address)
71
    {
72
        $responseData = array(
73
            'saved' => false,
74
            'lead' => $lead
75
        );
76
77
        if ($this->getRequest()->getMethod() == 'GET' && !$address->getId()) {
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
            $address->setFirstName($lead->getFirstName());
79
            $address->setLastName($lead->getLastName());
80
            if (!$lead->getAddresses()->count()) {
81
                $address->setPrimary(true);
82
            }
83
        }
84
85
        if ($address->getOwner() && $address->getOwner()->getId() != $lead->getId()) {
86
            throw new BadRequestHttpException('Address must belong to lead');
87
        } elseif (!$address->getOwner()) {
88
            $lead->addAddress($address);
89
        }
90
91
        // Update lead's modification date when an address is changed
92
        $lead->setUpdatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
93
94
        if ($this->get('orocrm_sales.lead_address.form.handler')->process($address)) {
95
            $this->getDoctrine()->getManager()->flush();
96
            $responseData['entity'] = $address;
97
            $responseData['saved'] = true;
98
        }
99
100
        $responseData['form'] = $this->get('orocrm_sales.lead_address.form')->createView();
101
102
        return $responseData;
103
    }
104
}
105