Completed
Pull Request — master (#1746)
by Kentaro
109:00 queued 102:48
created

DeliveryController::edit()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 72
Code Lines 42

Duplication

Lines 7
Ratio 9.72 %

Code Coverage

Tests 44
CRAP Score 6.0003

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 72
ccs 44
cts 45
cp 0.9778
rs 8.5164
cc 6
eloc 42
nc 9
nop 3
crap 6.0003

How to fix   Long Method   

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\Mypage;
26
27
use Eccube\Application;
28
use Eccube\Controller\AbstractController;
29
use Eccube\Event\EccubeEvents;
30
use Eccube\Event\EventArgs;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
34
class DeliveryController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
35
{
36
    /**
37
     * お届け先一覧画面.
38
     *
39
     * @param Application $app
40
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
41
     * @return \Symfony\Component\HttpFoundation\Response
42
     */
43 1
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
44
    {
45 1
        $Customer = $app['user'];
46
47 1
        return $app->render('Mypage/delivery.twig', array(
48 1
            'Customer' => $Customer,
49 1
        ));
50
    }
51
52
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
53
     * お届け先編集画面.
54
     *
55
     * @param Application $app
56
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
57
     * @return \Symfony\Component\HttpFoundation\Response
58
     */
59 12
    public function edit(Application $app, Request $request, $id = null)
60
    {
61 8
        $Customer = $app['user'];
62
63
        // 配送先住所最大値判定
64
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
65 12 View Code Duplication
        if (is_null($id)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66 4
            $addressCurrNum = count($Customer->getCustomerAddresses());
67 4
            $addressMax = $app['config']['deliv_addr_max'];
68 4
            if ($addressCurrNum >= $addressMax) {
69
                throw new NotFoundHttpException();
70
            }
71 4
        }
72
73 8
        $CustomerAddress = $app['eccube.repository.customer_address']->findOrCreateByCustomerAndId($Customer, $id);
74
75 8
        $parentPage = $request->get('parent_page', null);
76
77
        // 正しい遷移かをチェック
78
        $allowdParents = array(
79 8
            $app->url('mypage_delivery'),
80 8
            $app->url('shopping_delivery'),
81 8
        );
82
83
        // 遷移が正しくない場合、デフォルトであるマイページの配送先追加の画面を設定する
84 8
        if (!in_array($parentPage, $allowdParents)) {
85 8
            $parentPage  = $app->url('mypage_delivery');
86 8
        }
87
88 8
        $builder = $app['form.factory']
89 8
            ->createBuilder('customer_address', $CustomerAddress);
90
91 8
        $event = new EventArgs(
92
            array(
93 8
                'builder' => $builder,
94 8
                'Customer' => $Customer,
95 8
                'CustomerAddress' => $CustomerAddress,
96 8
            ),
97
            $request
98 8
        );
99 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_INITIALIZE, $event);
100
101 8
        $form = $builder->getForm();
102 8
        $form->handleRequest($request);
103
104 8
        if ($form->isSubmitted() && $form->isValid()) {
105 4
            $app['orm.em']->persist($CustomerAddress);
106 4
            $app['orm.em']->flush();
107
108 4
            $event = new EventArgs(
109
                array(
110 4
                    'form' => $form,
111 4
                    'Customer' => $Customer,
112 4
                    'CustomerAddress' => $CustomerAddress,
113 4
                ),
114
                $request
115 4
            );
116 4
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_COMPLETE, $event);
117
118 4
            $app->addSuccess('mypage.delivery.add.complete');
119
120 4
            return $app->redirect($app->url('mypage_delivery'));
121
        }
122
123 4
        $BaseInfo = $app['eccube.repository.base_info']->get();
124
125 4
        return $app->render('Mypage/delivery_edit.twig', array(
126 4
            'form' => $form->createView(),
127 4
            'parentPage' => $parentPage,
128 4
            'BaseInfo' => $BaseInfo,
129 4
        ));
130
    }
131
132
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
133
     * お届け先を削除する.
134
     *
135
     * @param Application $app
136
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
137
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
138
     */
139 4
    public function delete(Application $app, Request $request, $id)
140
    {
141 4
        $this->isTokenValid($app);
142
143 4
        $Customer = $app['user'];
144
145 4
        $status = $app['eccube.repository.customer_address']->deleteByCustomerAndId($Customer, $id);
146
147 4
        if ($status) {
148 2
            $event = new EventArgs(
149
                array(
150 2
                    'id' => $id,
151 2
                    'Customer' => $Customer,
152 2
                ), $request
153 2
            );
154 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_DELETE_COMPLETE, $event);
155
156 2
            $app->addSuccess('mypage.address.delete.complete');
157
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
158 2
        } else {
159 2
            $app->addError('mypage.address.delete.failed');
160
        }
161
162 4
        return $app->redirect($app->url('mypage_delivery'));
163
    }
164
}
165