Completed
Push — experimental/3.1 ( 878c99...05b497 )
by chihiro
50:03
created

DeliveryController::delete()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 3
nop 3
dl 0
loc 33
ccs 19
cts 19
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Setting\Shop;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Inject;
29
use Eccube\Application;
30
use Eccube\Controller\AbstractController;
31
use Eccube\Entity\Delivery;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Admin\DeliveryType;
35
use Eccube\Repository\DeliveryFeeRepository;
36
use Eccube\Repository\DeliveryRepository;
37
use Eccube\Repository\Master\PrefRepository;
38
use Eccube\Repository\PaymentOptionRepository;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
41
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
42
use Symfony\Component\EventDispatcher\EventDispatcher;
43
use Symfony\Component\Form\FormFactory;
44
use Symfony\Component\HttpFoundation\Request;
45
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
46
47
/**
48
 * @Route(service=DeliveryController::class)
49
 */
50
class DeliveryController extends AbstractController
51
{
52
    /**
53
     * @Inject(PaymentOptionRepository::class)
54
     * @var PaymentOptionRepository
55
     */
56
    protected $paymentOptionRepository;
57
58
    /**
59
     * @Inject("orm.em")
60
     * @var EntityManager
61
     */
62
    protected $entityManager;
63
64
    /**
65
     * @Inject("form.factory")
66
     * @var FormFactory
67
     */
68
    protected $formFactory;
69
70
    /**
71
     * @Inject(DeliveryFeeRepository::class)
72
     * @var DeliveryFeeRepository
73
     */
74
    protected $deliveryFeeRepository;
75
76
    /**
77
     * @Inject(PrefRepository::class)
78
     * @var PrefRepository
79
     */
80
    protected $prefRepository;
81
82
    /**
83
     * @Inject("eccube.event.dispatcher")
84
     * @var EventDispatcher
85
     */
86
    protected $eventDispatcher;
87
88
    /**
89
     * @Inject(DeliveryRepository::class)
90
     * @var DeliveryRepository
91
     */
92
    protected $deliveryRepository;
93
94
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
95
     * @Route("/{_admin}/setting/shop/delivery", name="admin_setting_shop_delivery")
96
     * @Template("Setting/Shop/delivery.twig")
97
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
98 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method 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...
99 1
    {
100
        $Deliveries = $this->deliveryRepository
101 1
            ->findBy([], ['sort_no' => 'DESC']);
102 1
103
        $event = new EventArgs(
104 1
            array(
105
                'Deliveries' => $Deliveries,
106 1
            ),
107
            $request
108 1
        );
109
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_INDEX_COMPLETE, $event);
110 1
111
        return [
112
            'Deliveries' => $Deliveries,
113 1
        ];
114
    }
115
116
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Delivery" missing
Loading history...
117
     * @Route("/{_admin}/setting/shop/delivery/new", name="admin_setting_shop_delivery_new")
118
     * @Route("/{_admin}/setting/shop/delivery/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_delivery_edit")
119
     * @Template("Setting/Shop/delivery_edit.twig")
120
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
121
    public function edit(Application $app, Request $request, Delivery $Delivery = null)
122 6
    {
123
        if (is_null($Delivery)) {
124 6
            // FIXME
125
            $Delivery = $this->deliveryRepository
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Repository\Delive...ository::findOrCreate() has been deprecated with message: 呼び出し元で制御する

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...
126 3
                ->findOrCreate(0);
127 3
        }
128
129
        // FormType: DeliveryFeeの生成
130
        $Prefs = $this->prefRepository
131 6
            ->findAll();
132 6
133
        foreach ($Prefs as $Pref) {
134 6
            $DeliveryFee = $this->deliveryFeeRepository
135 6
                ->findOrCreate(
136 6
                    array(
137
                        'Delivery' => $Delivery,
138 6
                        'Pref' => $Pref,
139 6
                    )
140
                );
141
            if (!$DeliveryFee->getFee()) {
142 6
                $Delivery->addDeliveryFee($DeliveryFee);
143 6
            }
144
        }
145
146
        $DeliveryFees = $Delivery->getDeliveryFees();
147 6
        $DeliveryFeesIndex = array();
148 6
        foreach ($DeliveryFees as $DeliveryFee) {
149 6
            $Delivery->removeDeliveryFee($DeliveryFee);
150 6
            $DeliveryFeesIndex[$DeliveryFee->getPref()->getId()] = $DeliveryFee;
151 6
        }
152
        ksort($DeliveryFeesIndex);
153 6
        foreach ($DeliveryFeesIndex as $timeId => $DeliveryFee) {
154 6
            $Delivery->addDeliveryFee($DeliveryFee);
155 6
        }
156
157
        // FormType: DeliveryTimeの生成
158
        $DeliveryTimes = $Delivery->getDeliveryTimes();
159 6
        $loop = 16 - count($DeliveryTimes);
160 6
        for ($i = 1; $i <= $loop; $i++) {
161 6
            $DeliveryTime = new \Eccube\Entity\DeliveryTime();
162 6
            $DeliveryTime->setDelivery($Delivery);
163 6
            $Delivery->addDeliveryTime($DeliveryTime);
164 6
        }
165
166
        $builder = $this->formFactory
167 6
            ->createBuilder(DeliveryType::class, $Delivery);
168 6
169
        $event = new EventArgs(
170 6
            array(
171
                'builder' => $builder,
172 6
                'Delivery' => $Delivery,
173 6
                'Prefs' => $Prefs,
174 6
                'DeliveryFees' => $DeliveryFees,
175 6
            ),
176
            $request
177 6
        );
178
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_INITIALIZE, $event);
179 6
180
        $form = $builder->getForm();
181 6
182
        // 支払方法をセット
183
        $Payments = array();
184 6
        foreach ($Delivery->getPaymentOptions() as $PaymentOption) {
185 6
            $Payments[] = $PaymentOption->getPayment();
186 3
        }
187
188
        $form['delivery_times']->setData($Delivery->getDeliveryTimes());
189 6
        $form['payments']->setData($Payments);
190 6
191
        // 登録ボタン押下
192
        if ($request->getMethod() === 'POST') {
193 6
            $form->handleRequest($request);
194 4
195
            if ($form->isValid()) {
196 4
                $DeliveryData = $form->getData();
197 2
198
                // 配送時間の登録
199
                $DeliveryTimes = $form['delivery_times']->getData();
200 2
                foreach ($DeliveryTimes as $DeliveryTime) {
201 2
                    if (is_null($DeliveryTime->getDeliveryTime())) {
202 2
                        $Delivery->removeDeliveryTime($DeliveryTime);
203 2
                        $this->entityManager->remove($DeliveryTime);
204 2
                    }
205
                }
206
207
                // お支払いの登録
208
                $PaymentOptions = $this->paymentOptionRepository
209 2
                    ->findBy(array('delivery_id' => $Delivery->getId()));
210 2
                // 消す
211
                foreach ($PaymentOptions as $PaymentOption) {
212 2
                    $DeliveryData->removePaymentOption($PaymentOption);
213 1
                    $this->entityManager->remove($PaymentOption);
214 1
                }
215
                $this->entityManager->persist($DeliveryData);
216 2
                $this->entityManager->flush();
217 2
218
                // いれる
219
                $PaymentsData = $form->get('payments')->getData();
220 2
                foreach ($PaymentsData as $PaymentData) {
221 2
                    $PaymentOption = new \Eccube\Entity\PaymentOption();
222 2
                    $PaymentOption
223
                        ->setPaymentId($PaymentData->getId())
224 2
                        ->setPayment($PaymentData)
225 2
                        ->setDeliveryId($DeliveryData->getId())
226 2
                        ->setDelivery($DeliveryData);
227 2
                    $DeliveryData->addPaymentOption($PaymentOption);
228 2
                    $this->entityManager->persist($DeliveryData);
229 2
                }
230
231
                $this->entityManager->persist($DeliveryData);
232 2
233
                $this->entityManager->flush();
234 2
235
                $event = new EventArgs(
236 2
                    array(
237
                        'form' => $form,
238 2
                        'Delivery' => $Delivery,
239 2
                        'Prefs' => $Prefs,
240 2
                        'DeliveryFees' => $DeliveryFees,
241 2
                    ),
242
                    $request
243 2
                );
244
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_COMPLETE, $event);
245 2
246
                $app->addSuccess('admin.register.complete', 'admin');
247 2
248
                return $app->redirect($app->url('admin_setting_shop_delivery'));
249 2
            }
250
        }
251
252
        return [
253
            'form' => $form->createView(),
254 4
            'delivery_id' => $Delivery->getId(),
255 4
        ];
256
    }
257
258
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Delivery" missing
Loading history...
259
     * @Method("DELETE")
260
     * @Route("/{_admin}/setting/shop/delivery/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_delivery_delete")
261
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
262
    public function delete(Application $app, Request $request, Delivery $Delivery)
263 1
    {
264
        $this->isTokenValid($app);
265 1
266
        $this->entityManager->remove($Delivery);
267 1
        $this->entityManager->flush($Delivery);
268 1
269
        $sortNo = 1;
270 1
        $Delivs = $this->deliveryRepository
271 1
            ->findBy([], ['sort_no' => 'ASC']);
272 1
273
        foreach ($Delivs as $Deliv) {
274 1
            if ($Deliv->getId() != $Delivery->getId()) {
275 1
                $Deliv->setSortNo($sortNo);
276 1
                $sortNo++;
277 1
            }
278
        }
279
280
        $this->entityManager->flush();
281 1
282
        $event = new EventArgs(
283 1
            array(
284
                'Delivs' => $Delivs,
285 1
                'Delivery' => $Delivery,
286 1
            ),
287
            $request
288 1
        );
289
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_DELETE_COMPLETE, $event);
290 1
291
        $app->addSuccess('admin.delete.complete', 'admin');
292 1
293
        return $app->redirect($app->url('admin_setting_shop_delivery'));
294 1
    }
295
296
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Delivery" missing
Loading history...
297
     * @Method("PUT")
298
     * @Route("/{_admin}/setting/shop/delivery/{id}/visibility", requirements={"id" = "\d+"}, name="admin_setting_shop_delivery_visibility")
299
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
300
    public function visibility(Application $app, Request $request, Delivery $Delivery)
301
    {
302
        $this->isTokenValid($app);
303
304
        // 表示・非表示を切り替える
305
        if ($Delivery->isVisible()) {
306
            $message = 'admin.delivery.hidden.complete';
307
            $Delivery->setVisible(false);
308
        } else {
309
            $message = 'admin.delivery.visible.complete';
310
            $Delivery->setVisible(true);
311
        }
312
        $this->entityManager->persist($Delivery);
313
314
        $this->entityManager->flush($Delivery);
315
316
        $event = new EventArgs(
317
            array(
318
                'Delivery' => $Delivery,
319
            ),
320
            $request
321
        );
322
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_VISIBILITY_COMPLETE, $event);
323
324
        $app->addSuccess($message, 'admin');
325
326
        return $app->redirect($app->url('admin_setting_shop_delivery'));
327
    }
328
329
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
330
     * @Method("POST")
331
     * @Route("/{_admin}/setting/shop/delivery/sort_no/move", name="admin_setting_shop_delivery_sort_no_move")
332
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
333 1 View Code Duplication
    public function moveSortNo(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method 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...
334
    {
335 1
        if (!$request->isXmlHttpRequest()) {
336
            throw new BadRequestHttpException();
337
        }
338
339 1
        $sortNos = $request->request->all();
340 1
        foreach ($sortNos as $deliveryId => $sortNo) {
341 1
            $Delivery = $this->deliveryRepository
342 1
                ->find($deliveryId);
343 1
            $Delivery->setSortNo($sortNo);
344
        }
345 1
        $this->entityManager->flush();
346
347 1
        return true;
348
    }
349
}
350