DeliveryController::edit()   F
last analyzed

Complexity

Conditions 13
Paths 672

Size

Total Lines 131
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 83
nc 672
nop 3
dl 0
loc 131
rs 2.4575
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Event\EccubeEvents;
31
use Eccube\Event\EventArgs;
32
use Symfony\Component\HttpFoundation\Request;
33
34
class DeliveryController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    private $main_title;
0 ignored issues
show
Unused Code introduced by
The property $main_title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
    private $sub_title;
0 ignored issues
show
Unused Code introduced by
The property $sub_title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
38
39
    public $form;
40
41
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
42
    {
43
    }
44
45 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
46
    {
47
        $Deliveries = $app['eccube.repository.delivery']
48
            ->findBy(
49
                array('del_flg' => 0),
50
                array('rank' => 'DESC')
51
            );
52
53
        $event = new EventArgs(
54
            array(
55
                'Deliveries' => $Deliveries,
56
            ),
57
            $request
58
        );
59
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_INDEX_COMPLETE, $event);
60
61
        return $app->render('Setting/Shop/delivery.twig', array(
62
            'Deliveries' => $Deliveries,
63
        ));
64
    }
65
66
    public function edit(Application $app, Request $request, $id = 0)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
67
    {
68
        /* @var $Delivery \Eccube\Entity\Delivery */
69
        $Delivery = $app['eccube.repository.delivery']
70
            ->findOrCreate($id);
71
72
        // FormType: DeliveryFeeの生成
73
        $Prefs = $app['eccube.repository.master.pref']
74
            ->findAll();
75
76
        foreach ($Prefs as $Pref) {
77
            $DeliveryFee = $app['eccube.repository.delivery_fee']
78
                ->findOrCreate(array(
79
                    'Delivery' => $Delivery,
80
                    'Pref' => $Pref,
81
                ));
82
            if (!$DeliveryFee->getFee()) {
83
                $Delivery->addDeliveryFee($DeliveryFee);
84
            }
85
        }
86
87
        $DeliveryFees = $Delivery->getDeliveryFees();
88
        $DeliveryFeesIndex = array();
89
        foreach ($DeliveryFees as $DeliveryFee) {
90
            $Delivery->removeDeliveryFee($DeliveryFee);
91
            $DeliveryFeesIndex[$DeliveryFee->getPref()->getId()] = $DeliveryFee;
92
        }
93
        ksort($DeliveryFeesIndex);
94
        foreach ($DeliveryFeesIndex as $timeId => $DeliveryFee) {
95
            $Delivery->addDeliveryFee($DeliveryFee);
96
        }
97
98
        // FormType: DeliveryTimeの生成
99
        $DeliveryTimes = $Delivery->getDeliveryTimes();
100
        $loop = 16 - count($DeliveryTimes);
101
        for ($i = 1; $i <= $loop; $i++) {
102
            $DeliveryTime = new \Eccube\Entity\DeliveryTime();
103
            $DeliveryTime->setDelivery($Delivery);
104
            $Delivery->addDeliveryTime($DeliveryTime);
105
        }
106
107
        $builder = $app['form.factory']
108
            ->createBuilder('delivery', $Delivery);
109
110
        $event = new EventArgs(
111
            array(
112
                'builder' => $builder,
113
                'Delivery' => $Delivery,
114
                'Prefs' => $Prefs,
115
                'DeliveryFees' => $DeliveryFees,
116
            ),
117
            $request
118
        );
119
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_INITIALIZE, $event);
120
121
        $form = $builder->getForm();
122
123
        // 支払方法をセット
124
        $Payments = array();
125
        foreach ($Delivery->getPaymentOptions() as $PaymentOption) {
126
            $Payments[] = $PaymentOption->getPayment();
127
        }
128
129
        $form['delivery_times']->setData($Delivery->getDeliveryTimes());
130
        $form['payments']->setData($Payments);
131
132
        // 登録ボタン押下
133
        if ($app['request']->getMethod() === 'POST') {
134
            $form->handleRequest($app['request']);
135
136
            if ($form->isValid()) {
137
                $DeliveryData = $form->getData();
138
139
                // 配送時間の登録
140
                $DeliveryTimes = $form['delivery_times']->getData();
141
                foreach ($DeliveryTimes as $DeliveryTime) {
142
                    if (is_null($DeliveryTime->getDeliveryTime())) {
143
                        $Delivery->removeDeliveryTime($DeliveryTime);
144
                        $app['orm.em']->remove($DeliveryTime);
145
                    }
146
                }
147
148
                // お支払いの登録
149
                $PaymentOptions = $app['eccube.repository.payment_option']
150
                    ->findBy(array('delivery_id' => $id));
151
                // 消す
152
                foreach ($PaymentOptions as $PaymentOption) {
153
                    $DeliveryData->removePaymentOption($PaymentOption);
154
                    $app['orm.em']->remove($PaymentOption);
155
                }
156
                $app['orm.em']->persist($DeliveryData);
157
                $app['orm.em']->flush();
158
159
                // いれる
160
                $PaymentsData = $form->get('payments')->getData();
161
                foreach ($PaymentsData as $PaymentData) {
162
                    $PaymentOption = new \Eccube\Entity\PaymentOption();
163
                    $PaymentOption
164
                        ->setPaymentId($PaymentData->getId())
165
                        ->setPayment($PaymentData)
166
                        ->setDeliveryId($DeliveryData->getId())
167
                        ->setDelivery($DeliveryData);
168
                    $DeliveryData->addPaymentOption($PaymentOption);
169
                    $app['orm.em']->persist($DeliveryData);
170
                }
171
172
                $app['orm.em']->persist($DeliveryData);
173
174
                $app['orm.em']->flush();
175
176
                $event = new EventArgs(
177
                    array(
178
                        'form' => $form,
179
                        'Delivery' => $Delivery,
180
                        'Prefs' => $Prefs,
181
                        'DeliveryFees' => $DeliveryFees,
182
                    ),
183
                    $request
184
                );
185
                $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_EDIT_COMPLETE, $event);
186
187
                $app->addSuccess('admin.register.complete', 'admin');
188
189
                return $app->redirect($app->url('admin_setting_shop_delivery'));
190
            }
191
        }
192
        return $app->render('Setting/Shop/delivery_edit.twig', array(
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
193
            'form' => $form->createView(),
194
            'delivery_id' => $id,
195
        ));
196
    }
197
198
    public function delete(Application $app, Request $request, $id)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
199
    {
200
        $this->isTokenValid($app);
201
202
        $repo = $app['eccube.repository.delivery'];
203
        $Delivery = $repo->find($id);
204
        if (!$Delivery) {
205
            $app->deleteMessage();
206
            return $app->redirect($app->url('admin_setting_shop_delivery'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
207
        }
208
209
        $Delivery
210
            ->setDelFlg(Constant::ENABLED)
211
            ->setRank(0);
212
213
        $app['orm.em']->persist($Delivery);
214
215
        $rank = 1;
216
        $Delivs = $repo
217
            ->findBy(
218
                array('del_flg' => Constant::DISABLED),
219
                array('rank' => 'ASC')
220
            );
221
        foreach ($Delivs as $Deliv) {
222
            if ($Deliv->getId() != $id) {
223
                $Deliv->setRank($rank);
224
                $rank++;
225
            }
226
        }
227
228
        $app['orm.em']->flush();
229
230
        $event = new EventArgs(
231
            array(
232
                'Delivs' => $Delivs,
233
                'Delivery' => $Delivery,
234
            ),
235
            $request
236
        );
237
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_DELIVERY_DELETE_COMPLETE, $event);
238
239
        $app->addSuccess('admin.delete.complete', 'admin');
240
241
        return $app->redirect($app->url('admin_setting_shop_delivery'));
242
    }
243
244 View Code Duplication
    public function moveRank(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
245
    {
246
        if ($request->isXmlHttpRequest()) {
247
            $ranks = $request->request->all();
248
            foreach ($ranks as $deliveryId => $rank) {
249
                $Delivery = $app['eccube.repository.delivery']
250
                    ->find($deliveryId);
251
                $Delivery->setRank($rank);
252
                $app['orm.em']->persist($Delivery);
253
            }
254
            $app['orm.em']->flush();
255
        }
256
257
        return true;
258
    }
259
}
260