Completed
Pull Request — master (#1779)
by Kentaro
214:45 queued 207:18
created

DeliveryController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 226
Duplicated Lines 15.49 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 0
cbo 5
dl 35
loc 226
ccs 153
cts 153
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A index() 20 20 1
F edit() 0 131 13
B delete() 0 45 4
A moveRank() 15 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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