DeliveryController::edit()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 77
Code Lines 44

Duplication

Lines 7
Ratio 9.09 %

Code Coverage

Tests 38
CRAP Score 6.0006

Importance

Changes 0
Metric Value
cc 6
eloc 44
nc 9
nop 3
dl 7
loc 77
ccs 38
cts 39
cp 0.9744
crap 6.0006
rs 8.4456
c 0
b 0
f 0

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
        ));
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 8
    public function edit(Application $app, Request $request, $id = null)
60
    {
61 8
        $Customer = $app['user'];
62
63
        // 配送先住所最大値判定
64
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
65 8 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
        }
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
        );
82
83
        // 遷移が正しくない場合、デフォルトであるマイページの配送先追加の画面を設定する
84 8
        if (!in_array($parentPage, $allowdParents)) {
85
            // @deprecated 使用されていないコード
86 8
            $parentPage  = $app->url('mypage_delivery');
87
        }
88
89 8
        $builder = $app['form.factory']
90 8
            ->createBuilder('customer_address', $CustomerAddress);
91
92 8
        $event = new EventArgs(
93
            array(
94 8
                'builder' => $builder,
95 8
                'Customer' => $Customer,
96 8
                'CustomerAddress' => $CustomerAddress,
97
            ),
98
            $request
99
        );
100 8
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_INITIALIZE, $event);
101
102 8
        $form = $builder->getForm();
103 8
        $form->handleRequest($request);
104
105 8
        if ($form->isSubmitted() && $form->isValid()) {
106 4
            log_info('お届け先登録開始', array($id));
107
108 4
            $app['orm.em']->persist($CustomerAddress);
109 4
            $app['orm.em']->flush();
110
111 4
            log_info('お届け先登録完了', array($id));
112
113 4
            $event = new EventArgs(
114
                array(
115 4
                    'form' => $form,
116 4
                    'Customer' => $Customer,
117 4
                    'CustomerAddress' => $CustomerAddress,
118
                ),
119
                $request
120
            );
121 4
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_COMPLETE, $event);
122
123 4
            $app->addSuccess('mypage.delivery.add.complete');
124
125 4
            return $app->redirect($app->url('mypage_delivery'));
126
        }
127
128 4
        $BaseInfo = $app['eccube.repository.base_info']->get();
129
130 4
        return $app->render('Mypage/delivery_edit.twig', array(
131 4
            'form' => $form->createView(),
132 4
            'parentPage' => $parentPage,
133 4
            'BaseInfo' => $BaseInfo,
134
        ));
135
    }
136
137
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
138
     * お届け先を削除する.
139
     *
140
     * @param Application $app
141
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
142
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
143
     */
144 4
    public function delete(Application $app, Request $request, $id)
145
    {
146 4
        $this->isTokenValid($app);
147
148 4
        log_info('お届け先削除開始', array($id));
149
150 4
        $Customer = $app['user'];
151
152 4
        $status = $app['eccube.repository.customer_address']->deleteByCustomerAndId($Customer, $id);
153
154 4 View Code Duplication
        if ($status) {
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...
155 2
            $event = new EventArgs(
156
                array(
157 2
                    'id' => $id,
158 2
                    'Customer' => $Customer,
159
                ), $request
160
            );
161 2
            $app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_DELETE_COMPLETE, $event);
162
163 2
            $app->addSuccess('mypage.address.delete.complete');
164
165 2
            log_info('お届け先削除完了', array($id));
166
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
167
        } else {
168 2
            $app->addError('mypage.address.delete.failed');
169
170 2
            log_info('お届け先削除失敗', array($id));
171
        }
172
173 4
        return $app->redirect($app->url('mypage_delivery'));
174
    }
175
}
176