SchedulesController   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 260
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 1
dl 0
loc 260
rs 9.44
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getListRenderTemplate() 0 7 1
A redirectToListTemplate() 0 7 1
A getEditRenderTemplate() 0 4 1
A getCreateRenderTemplate() 0 4 1
A getObjectId() 0 5 1
A getExistingObject() 0 4 1
A hydrateObjectForm() 0 4 1
A getService() 0 8 2
F createAction() 0 84 15
A formatData() 0 16 5
A hasNullDate() 0 4 2
B cloneAction() 0 52 5
A getCloneForm() 0 4 1
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;
18
use Dealer\Model\DealerShedules;
19
use Propel\Runtime\Propel;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Thelia\Core\Security\AccessManager;
22
use Thelia\Core\Security\Resource\AdminResources;
23
use Thelia\Form\Exception\FormValidationException;
24
use Thelia\Tools\URL;
25
26
/**
27
 * Class SchedulesController
28
 * @package Dealer\Controller
29
 */
30
class SchedulesController extends BaseController
31
{
32
    const CONTROLLER_ENTITY_NAME = "dealer-schedules";
33
    const CONTROLLER_CHECK_RESOURCE = Dealer::RESOURCES_SCHEDULES;
34
35
    /**
36
     * Use to get render of list
37
     * @return mixed
38
     */
39
    protected function getListRenderTemplate()
40
    {
41
        $id = $this->getRequest()->request->get("dealer_id");
42
43
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
44
            ["dealer_id" => $id, ]));
45
    }
46
47
    /**
48
     * Must return a RedirectResponse instance
49
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
50
     */
51
    protected function redirectToListTemplate()
52
    {
53
        $id = $this->getRequest()->request->get("dealer_id");
54
55
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
56
            ["dealer_id" => $id, ]));
57
    }
58
59
    /**
60
     * Use to get Edit render
61
     * @return mixed
62
     */
63
    protected function getEditRenderTemplate()
64
    {
65
        return $this->render("dealer-edit");
66
    }
67
68
    /**
69
     * Use to get Create render
70
     * @return mixed
71
     */
72
    protected function getCreateRenderTemplate()
73
    {
74
        return $this->render("dealer-edit");
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    protected function getObjectId($object)
81
    {
82
        /** @var DealerShedules $object */
83
        $object->getId();
84
    }
85
86
    /**
87
     * Load an existing object from the database
88
     */
89
    protected function getExistingObject()
90
    {
91
        // TODO: Implement getExistingObject() method.
92
    }
93
94
    /**
95
     * Hydrate the update form for this object, before passing it to the update template
96
     *
97
     * @param mixed $object
98
     */
99
    protected function hydrateObjectForm($object)
100
    {
101
        // TODO: Implement hydrateObjectForm() method.
102
    }
103
104
    /**
105
     * Method to get current controller associated service
106
     * @return object
107
     */
108
    protected function getService()
109
    {
110
        if (!$this->service) {
111
            $this->service = $this->getContainer()->get("dealer_schedules_service");
112
        }
113
114
        return $this->service;
115
    }
116
117
    /**
118
     * Create an object
119
     * @return mixed|\Symfony\Component\HttpFoundation\Response
120
     */
121
    public function createAction()
122
    {
123
        // Check current user authorization
124
        if (null !== $response = $this->checkAuth(self::CONTROLLER_CHECK_RESOURCE, Dealer::getModuleCode(),
125
                AccessManager::CREATE)
126
        ) {
127
            return $response;
128
        }
129
130
        // Create the Creation Form
131
        $creationForm = $this->getCreationForm($this->getRequest());
132
133
        $con = Propel::getConnection();
134
        $con->beginTransaction();
135
136
        try {
137
            // Check the form against constraints violations
138
            $form = $this->validateForm($creationForm, "POST");
139
            // Get the form field values
140
            $data = $form->getData();
141
142
            if (empty($data["day"])) {
143
                $dataAM = $this->formatData($data);
144
                $dataPM = $this->formatData($data, 'PM');
145
146
                if ($this->hasNullDate($dataAM) && $this->hasNullDate($dataPM)) {
147
                    $this->getService()->createFromArray($dataAM, $this->getCurrentEditionLocale());
148
                } else {
149
                    if (!$this->hasNullDate($dataAM)) {
150
                        $this->getService()->createFromArray($dataAM, $this->getCurrentEditionLocale());
151
                    }
152
                    if (!$this->hasNullDate($dataPM)) {
153
                        $this->getService()->createFromArray($dataPM, $this->getCurrentEditionLocale());
154
                    }
155
                }
156
            } else {
157
                foreach ($data["day"] as $day) {
158
                    $currentData = $data;
159
                    $currentData["day"] = $day;
160
                    $dataAM = $this->formatData($currentData);
161
                    $dataPM = $this->formatData($currentData, 'PM');
162
163
                    if ($this->hasNullDate($dataAM) && $this->hasNullDate($dataPM)) {
164
                        $this->getService()->createFromArray($dataAM, $this->getCurrentEditionLocale());
165
                    } else {
166
                        if (!$this->hasNullDate($dataAM)) {
167
                            $this->getService()->createFromArray($dataAM, $this->getCurrentEditionLocale());
168
                        }
169
                        if (!$this->hasNullDate($dataPM)) {
170
                            $this->getService()->createFromArray($dataPM, $this->getCurrentEditionLocale());
171
                        }
172
                    }
173
                }
174
            }
175
176
177
            // Substitute _ID_ in the URL with the ID of the created object
178
            $successUrl = $creationForm->getSuccessUrl();
179
180
            $con->commit();
181
182
            // Redirect to the success URL
183
            return $this->generateRedirect($successUrl);
184
        } catch (FormValidationException $ex) {
0 ignored issues
show
Bug introduced by
The class Thelia\Form\Exception\FormValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
185
            $con->rollBack();
186
            // Form cannot be validated
187
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
188
        } catch (\Exception $ex) {
189
            $con->rollBack();
190
            // Any other error
191
            $error_msg = $ex->getMessage();
192
        }
193
        if (false !== $error_msg) {
194
            $this->setupFormErrorContext(
195
                $this->getTranslator()->trans("%obj creation", ['%obj' => static::CONTROLLER_ENTITY_NAME]),
196
                $error_msg,
197
                $creationForm,
198
                $ex
199
            );
200
201
            // At this point, the form has error, and should be redisplayed.
202
            return $this->generateErrorRedirect($creationForm);
203
        }
204
    }
205
206
    protected function formatData($data, $type = "AM")
207
    {
208
        $retour = $data;
209
        if (isset($data["begin" . $type]) && $data["begin" . $type] != "") {
210
            $retour["begin"] = $data["begin" . $type];
211
        } else {
212
            $retour["begin"] = null;
213
        }
214
        if (isset($data["end" . $type]) && $data["end" . $type] != "") {
215
            $retour["end"] = $data["end" . $type];
216
        } else {
217
            $retour["end"] = null;
218
        }
219
220
        return $retour;
221
    }
222
223
    protected function hasNullDate($data)
224
    {
225
        return !($data["begin"] && $data["end"]);
226
    }
227
228
    public function cloneAction()
229
    {
230
        // Check current user authorization
231
        if (null !== $response = $this->checkAuth(self::CONTROLLER_CHECK_RESOURCE, Dealer::getModuleCode(),
232
                AccessManager::CREATE)
233
        ) {
234
            return $response;
235
        }
236
237
        // Create the Creation Form
238
        $cloneForm = $this->getCloneForm($this->getRequest());
239
240
        $con = Propel::getConnection();
241
        $con->beginTransaction();
242
243
        try {
244
            // Check the form against constraints violations
245
            $form = $this->validateForm($cloneForm, "POST");
246
            // Get the form field values
247
            $data = $form->getData();
248
249
            $this->getService()->cloneFromArray($data);
250
251
252
            // Substitute _ID_ in the URL with the ID of the created object
253
            $successUrl = $cloneForm->getSuccessUrl();
254
255
            $con->commit();
256
257
            // Redirect to the success URL
258
            return $this->generateRedirect($successUrl);
259
        } catch (FormValidationException $ex) {
0 ignored issues
show
Bug introduced by
The class Thelia\Form\Exception\FormValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
260
            $con->rollBack();
261
            // Form cannot be validated
262
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
263
        } catch (\Exception $ex) {
264
            $con->rollBack();
265
            // Any other error
266
            $error_msg = $ex->getMessage();
267
        }
268
        if (false !== $error_msg) {
269
            $this->setupFormErrorContext(
270
                $this->getTranslator()->trans("%obj creation", ['%obj' => static::CONTROLLER_ENTITY_NAME]),
271
                $error_msg,
272
                $cloneForm,
273
                $ex
274
            );
275
276
            // At this point, the form has error, and should be redisplayed.
277
            return $this->getListRenderTemplate();
278
        }
279
    }
280
281
    /**
282
     * Method to get Base Clone Form
283
     * @return \Thelia\Form\BaseForm
284
     */
285
    protected function getCloneForm()
286
    {
287
        return $this->createForm(static::CONTROLLER_ENTITY_NAME . ".clone");
288
    }
289
}
290