Failed Conditions
Push — experimental/3.1 ( 94d0cd...3ded70 )
by chihiro
23s
created

DeliveryController::visibility()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 17
nc 2
nop 3
dl 0
loc 27
ccs 0
cts 15
cp 0
crap 6
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\Component;
29
use Eccube\Annotation\Inject;
30
use Eccube\Application;
31
use Eccube\Common\Constant;
32
use Eccube\Controller\AbstractController;
33
use Eccube\Entity\Delivery;
34
use Eccube\Event\EccubeEvents;
35
use Eccube\Event\EventArgs;
36
use Eccube\Form\Type\Admin\DeliveryType;
37
use Eccube\Repository\DeliveryFeeRepository;
38
use Eccube\Repository\DeliveryRepository;
39
use Eccube\Repository\Master\PrefRepository;
40
use Eccube\Repository\PaymentOptionRepository;
41
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
42
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
43
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
44
use Symfony\Component\EventDispatcher\EventDispatcher;
45
use Symfony\Component\Form\FormFactory;
46
use Symfony\Component\HttpFoundation\Request;
47
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
48
49
/**
50
 * @Component
51
 * @Route(service=DeliveryController::class)
52
 */
53
class DeliveryController extends AbstractController
54
{
55
    /**
56
     * @Inject(PaymentOptionRepository::class)
57
     * @var PaymentOptionRepository
58
     */
59
    protected $paymentOptionRepository;
60
61
    /**
62
     * @Inject("orm.em")
63
     * @var EntityManager
64
     */
65
    protected $entityManager;
66
67
    /**
68
     * @Inject("form.factory")
69
     * @var FormFactory
70
     */
71
    protected $formFactory;
72
73
    /**
74
     * @Inject(DeliveryFeeRepository::class)
75
     * @var DeliveryFeeRepository
76
     */
77
    protected $deliveryFeeRepository;
78
79
    /**
80
     * @Inject(PrefRepository::class)
81
     * @var PrefRepository
82
     */
83
    protected $prefRepository;
84
85
    /**
86
     * @Inject("eccube.event.dispatcher")
87
     * @var EventDispatcher
88
     */
89
    protected $eventDispatcher;
90
91
    /**
92
     * @Inject(DeliveryRepository::class)
93
     * @var DeliveryRepository
94
     */
95
    protected $deliveryRepository;
96
97
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
98
     * @Route("/{_admin}/setting/shop/delivery", name="admin_setting_shop_delivery")
99
     * @Template("Setting/Shop/delivery.twig")
100
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
101 1 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...
102
    {
103 1
        $Deliveries = $this->deliveryRepository
104 1
            ->findBy([], ['rank' => 'DESC']);
105
106 1
        $event = new EventArgs(
107
            array(
108 1
                'Deliveries' => $Deliveries,
109
            ),
110 1
            $request
111
        );
112 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_INDEX_COMPLETE, $event);
113
114
        return [
115 1
            'Deliveries' => $Deliveries,
116
        ];
117
    }
118
119
    /**
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...
120
     * @Route("/{_admin}/setting/shop/delivery/new", name="admin_setting_shop_delivery_new")
121
     * @Route("/{_admin}/setting/shop/delivery/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_delivery_edit")
122
     * @Template("Setting/Shop/delivery_edit.twig")
123
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
124 6
    public function edit(Application $app, Request $request, Delivery $Delivery = null)
125
    {
126 6
        if (is_null($Delivery)) {
127
            // FIXME
128 3
            $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...
129 3
                ->findOrCreate(0);
130
        }
131
132
        // FormType: DeliveryFeeの生成
133 6
        $Prefs = $this->prefRepository
134 6
            ->findAll();
135
136 6
        foreach ($Prefs as $Pref) {
137 6
            $DeliveryFee = $this->deliveryFeeRepository
138 6
                ->findOrCreate(
139
                    array(
140 6
                        'Delivery' => $Delivery,
141 6
                        'Pref' => $Pref,
142
                    )
143
                );
144 6
            if (!$DeliveryFee->getFee()) {
145 6
                $Delivery->addDeliveryFee($DeliveryFee);
146
            }
147
        }
148
149 6
        $DeliveryFees = $Delivery->getDeliveryFees();
150 6
        $DeliveryFeesIndex = array();
151 6
        foreach ($DeliveryFees as $DeliveryFee) {
152 6
            $Delivery->removeDeliveryFee($DeliveryFee);
153 6
            $DeliveryFeesIndex[$DeliveryFee->getPref()->getId()] = $DeliveryFee;
154
        }
155 6
        ksort($DeliveryFeesIndex);
156 6
        foreach ($DeliveryFeesIndex as $timeId => $DeliveryFee) {
157 6
            $Delivery->addDeliveryFee($DeliveryFee);
158
        }
159
160
        // FormType: DeliveryTimeの生成
161 6
        $DeliveryTimes = $Delivery->getDeliveryTimes();
162 6
        $loop = 16 - count($DeliveryTimes);
163 6
        for ($i = 1; $i <= $loop; $i++) {
164 6
            $DeliveryTime = new \Eccube\Entity\DeliveryTime();
165 6
            $DeliveryTime->setDelivery($Delivery);
166 6
            $Delivery->addDeliveryTime($DeliveryTime);
167
        }
168
169 6
        $builder = $this->formFactory
170 6
            ->createBuilder(DeliveryType::class, $Delivery);
171
172 6
        $event = new EventArgs(
173
            array(
174 6
                'builder' => $builder,
175 6
                'Delivery' => $Delivery,
176 6
                'Prefs' => $Prefs,
177 6
                'DeliveryFees' => $DeliveryFees,
178
            ),
179 6
            $request
180
        );
181 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_INITIALIZE, $event);
182
183 6
        $form = $builder->getForm();
184
185
        // 支払方法をセット
186 6
        $Payments = array();
187 6
        foreach ($Delivery->getPaymentOptions() as $PaymentOption) {
188 3
            $Payments[] = $PaymentOption->getPayment();
189
        }
190
191 6
        $form['delivery_times']->setData($Delivery->getDeliveryTimes());
192 6
        $form['payments']->setData($Payments);
193
194
        // 登録ボタン押下
195 6
        if ($request->getMethod() === 'POST') {
196 4
            $form->handleRequest($request);
197
198 4
            if ($form->isValid()) {
199 2
                $DeliveryData = $form->getData();
200
201
                // 配送時間の登録
202 2
                $DeliveryTimes = $form['delivery_times']->getData();
203 2
                foreach ($DeliveryTimes as $DeliveryTime) {
204 2
                    if (is_null($DeliveryTime->getDeliveryTime())) {
205 2
                        $Delivery->removeDeliveryTime($DeliveryTime);
206 2
                        $this->entityManager->remove($DeliveryTime);
207
                    }
208
                }
209
210
                // お支払いの登録
211 2
                $PaymentOptions = $this->paymentOptionRepository
212 2
                    ->findBy(array('delivery_id' => $Delivery->getId()));
213
                // 消す
214 2
                foreach ($PaymentOptions as $PaymentOption) {
215 1
                    $DeliveryData->removePaymentOption($PaymentOption);
216 1
                    $this->entityManager->remove($PaymentOption);
217
                }
218 2
                $this->entityManager->persist($DeliveryData);
219 2
                $this->entityManager->flush();
220
221
                // いれる
222 2
                $PaymentsData = $form->get('payments')->getData();
223 2
                foreach ($PaymentsData as $PaymentData) {
224 2
                    $PaymentOption = new \Eccube\Entity\PaymentOption();
225
                    $PaymentOption
226 2
                        ->setPaymentId($PaymentData->getId())
227 2
                        ->setPayment($PaymentData)
228 2
                        ->setDeliveryId($DeliveryData->getId())
229 2
                        ->setDelivery($DeliveryData);
230 2
                    $DeliveryData->addPaymentOption($PaymentOption);
231 2
                    $this->entityManager->persist($DeliveryData);
232
                }
233
234 2
                $this->entityManager->persist($DeliveryData);
235
236 2
                $this->entityManager->flush();
237
238 2
                $event = new EventArgs(
239
                    array(
240 2
                        'form' => $form,
241 2
                        'Delivery' => $Delivery,
242 2
                        'Prefs' => $Prefs,
243 2
                        'DeliveryFees' => $DeliveryFees,
244
                    ),
245 2
                    $request
246
                );
247 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_COMPLETE, $event);
248
249 2
                $app->addSuccess('admin.register.complete', 'admin');
250
251 2
                return $app->redirect($app->url('admin_setting_shop_delivery'));
252
            }
253
        }
254
255
        return [
256 4
            'form' => $form->createView(),
257 4
            'delivery_id' => $Delivery->getId(),
258
        ];
259
    }
260
261
    /**
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...
262
     * @Method("DELETE")
263
     * @Route("/{_admin}/setting/shop/delivery/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_delivery_delete")
264
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
265 1
    public function delete(Application $app, Request $request, Delivery $Delivery)
266
    {
267 1
        $this->isTokenValid($app);
268
269 1
        $this->entityManager->remove($Delivery);
270 1
        $this->entityManager->flush($Delivery);
271
272 1
        $rank = 1;
273 1
        $Delivs = $this->deliveryRepository
274 1
            ->findBy([], ['rank' => 'ASC']);
275
276 1
        foreach ($Delivs as $Deliv) {
277 1
            if ($Deliv->getId() != $Delivery->getId()) {
278 1
                $Deliv->setRank($rank);
279 1
                $rank++;
280
            }
281
        }
282
283 1
        $this->entityManager->flush();
284
285 1
        $event = new EventArgs(
286
            array(
287 1
                'Delivs' => $Delivs,
288 1
                'Delivery' => $Delivery,
289
            ),
290 1
            $request
291
        );
292 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_DELETE_COMPLETE, $event);
293
294 1
        $app->addSuccess('admin.delete.complete', 'admin');
295
296 1
        return $app->redirect($app->url('admin_setting_shop_delivery'));
297
    }
298
299
    /**
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...
300
     * @Method("PUT")
301
     * @Route("/{_admin}/setting/shop/delivery/{id}/visibility", requirements={"id" = "\d+"}, name="admin_setting_shop_delivery_visibility")
302
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
303
    public function visibility(Application $app, Request $request, Delivery $Delivery)
304
    {
305
        $this->isTokenValid($app);
306
307
        $message = 'admin.delivery.visible.complete';
308
        if ($Delivery->isVisible()) {
309
            $Delivery->setVisible(false);
310
        } else {
311
            $message = 'admin.delivery.hidden.complete';
312
            $Delivery->setVisible(true);
313
        }
314
        $this->entityManager->persist($Delivery);
315
316
        $this->entityManager->flush($Delivery);
317
318
        $event = new EventArgs(
319
            array(
320
                'Delivery' => $Delivery,
321
            ),
322
            $request
323
        );
324
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_VISIBILITY_COMPLETE, $event);
325
326
        $app->addSuccess($message, 'admin');
327
328
        return $app->redirect($app->url('admin_setting_shop_delivery'));
329
    }
330
331
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
332
     * @Method("POST")
333
     * @Route("/{_admin}/setting/shop/delivery/rank/move", name="admin_setting_shop_delivery_rank_move")
334
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
335 1
    public function moveRank(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...
336
    {
337 1
        if (!$request->isXmlHttpRequest()) {
338
            throw new BadRequestHttpException();
339
        }
340
341 1
        $ranks = $request->request->all();
342 1
        foreach ($ranks as $deliveryId => $rank) {
343 1
            $Delivery = $this->deliveryRepository
344 1
                ->find($deliveryId);
345 1
            $Delivery->setRank($rank);
346
        }
347 1
        $this->entityManager->flush();
348
349 1
        return true;
350
    }
351
}
352