Completed
Push — master ( b7378d...ab1af8 )
by Antony
02:25
created

DealerController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 7
Bugs 0 Features 4
Metric Value
wmc 12
c 7
b 0
f 4
lcom 0
cbo 1
dl 0
loc 132
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getExistingObject() 0 4 1
A hydrateObjectForm() 0 18 1
A getListRenderTemplate() 0 4 1
A getEditRenderTemplate() 0 4 1
A getCreateRenderTemplate() 0 4 1
A getObjectId() 0 5 1
A redirectToListTemplate() 0 4 1
A redirectToEditionTemplate() 0 7 1
B toggleVisibleAction() 0 45 4
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
/*************************************************************************************/
13
14
namespace Dealer\Controller;
15
16
use Dealer\Controller\Base\BaseController;
17
use Dealer\Dealer as DealerModule;
18
use Dealer\Model\Dealer;
19
use Dealer\Model\DealerQuery;
20
use Propel\Runtime\Propel;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Thelia\Core\HttpFoundation\JsonResponse;
23
use Thelia\Core\Security\AccessManager;
24
use Thelia\Core\Security\Resource\AdminResources;
25
use Thelia\Tools\URL;
26
27
/**
28
 * Class DealerController
29
 * @package Dealer\Controller
30
 */
31
class DealerController extends BaseController
32
{
33
    /**
34
     * Load an existing object from the database
35
     */
36
    protected function getExistingObject()
37
    {
38
        return DealerQuery::create()->findPk($this->getRequest()->query->get("dealer_id"));
39
    }
40
41
    /**
42
     * Hydrate the update form for this object, before passing it to the update template
43
     *
44
     * @param mixed $object
45
     */
46
    protected function hydrateObjectForm($object)
47
    {
48
        $data = array(
49
            "id" => $object->getId(),
50
            "title" => $object->getTitle(),
51
            "address1" => $object->getAddress1(),
52
            "address2" => $object->getAddress2(),
53
            "address3" => $object->getAddress3(),
54
            "zipcode" => $object->getZipcode(),
55
            "city" => $object->getCity(),
56
            "country_id" => $object->getCountryId(),
57
            "description" => $object->getDescription(),
58
            "latitude" => $object->getLatitude(),
59
            "longitude" => $object->getLongitude(),
60
        );
61
62
        return $this->getUpdateForm($data);
63
    }
64
65
    const CONTROLLER_ENTITY_NAME = "dealer";
66
67
    /**
68
     * Use to get render of list
69
     * @return mixed
70
     */
71
    protected function getListRenderTemplate()
72
    {
73
        return $this->render("dealers");
74
    }
75
76
    /**
77
     * Use to get Edit render
78
     * @return mixed
79
     */
80
    protected function getEditRenderTemplate()
81
    {
82
        return $this->render("dealer-edit");
83
    }
84
85
    /**
86
     * Use to get Create render
87
     * @return mixed
88
     */
89
    protected function getCreateRenderTemplate()
90
    {
91
        // TODO: Implement getCreateRenderTemplate() method.
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    protected function getObjectId($object)
98
    {
99
        /** @var Dealer $object */
100
        return $object->getId();
101
    }
102
103
    protected function redirectToListTemplate()
104
    {
105
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer"));
106
    }
107
108
    protected function redirectToEditionTemplate()
109
    {
110
        $id = $this->getRequest()->query->get("dealer_id");
111
112
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
113
            ["dealer_id" => $id,]));
114
    }
115
116
    public function toggleVisibleAction($id)
117
    {
118
        // Check current user authorization
119
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, DealerModule::getModuleCode(),
120
                AccessManager::UPDATE)
121
        ) {
122
            return $response;
123
        }
124
125
        // Error (Default: false)
126
        $error_msg = false;
0 ignored issues
show
Unused Code introduced by
$error_msg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
128
        $retour = [];
129
        $code = 200;
130
131
132
        $con = Propel::getConnection();
133
        $con->beginTransaction();
134
135
        try {
136
137
            $updatedObject = $this->getService()->toggleVisibilityFromId($id);
138
139
            // Check if object exist
140
            if (!$updatedObject) {
141
                throw new \LogicException(
142
                    $this->getTranslator()->trans("No %obj was updated.", ['%obj' => static::CONTROLLER_ENTITY_NAME])
143
                );
144
            }
145
146
            $con->commit();
147
148
            $retour["message"] = "Visibility was updated";
149
150
151
        } catch (\Exception $ex) {
152
            $con->rollBack();
153
            // Any other error
154
            $retour["message"] = "Visibility can be updated";
155
            $code = 500;
156
            $retour["error"] = $ex->getMessage();
157
        }
158
159
        return JsonResponse::create($retour, $code);
160
    }
161
162
}