Failed Conditions
Pull Request — experimental/3.1 (#2449)
by Kiyotaka
52:40
created

DeliveryController::edit()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 77
Code Lines 44

Duplication

Lines 7
Ratio 9.09 %

Code Coverage

Tests 40
CRAP Score 6.0005

Importance

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