Completed
Pull Request — experimental/3.1 (#2484)
by Kentaro
57:08 queued 35:11
created

DeliveryController::edit()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 75
Code Lines 43

Duplication

Lines 7
Ratio 9.33 %

Code Coverage

Tests 38
CRAP Score 6.0006

Importance

Changes 0
Metric Value
cc 6
eloc 43
nc 9
nop 3
dl 7
loc 75
ccs 38
cts 39
cp 0.9744
crap 6.0006
rs 8.4736
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 Doctrine\ORM\EntityManager;
28
use Eccube\Annotation\Component;
29
use Eccube\Annotation\Inject;
30
use Eccube\Application;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Entity\BaseInfo;
33
use Eccube\Event\EccubeEvents;
34
use Eccube\Event\EventArgs;
35
use Eccube\Form\Type\Front\CustomerAddressType;
36
use Eccube\Repository\CustomerAddressRepository;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Component\EventDispatcher\EventDispatcher;
40
use Symfony\Component\Form\FormFactory;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
44
/**
45
 * @Component
46
 * @Route(service=DeliveryController::class)
47
 */
48
class DeliveryController extends AbstractController
49
{
50
    /**
51
     * @Inject(BaseInfo::class)
52
     * @var BaseInfo
53
     */
54
    protected $BaseInfo;
55
56
    /**
57
     * @Inject("orm.em")
58
     * @var EntityManager
59
     */
60
    protected $entityManager;
61
62
    /**
63
     * @Inject("eccube.event.dispatcher")
64
     * @var EventDispatcher
65
     */
66
    protected $eventDispatcher;
67
68
    /**
69
     * @Inject("form.factory")
70
     * @var FormFactory
71
     */
72
    protected $formFactory;
73
74
    /**
75
     * @Inject(CustomerAddressRepository::class)
76
     * @var CustomerAddressRepository
77
     */
78
    protected $customerAddressRepository;
79
80
    /**
81
     * @Inject("config")
82
     * @var array
83
     */
84
    protected $appConfig;
85
86
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
87
     * お届け先一覧画面.
88
     *
89
     * @Route("/mypage/delivery", name="mypage_delivery")
90
     * @Template("Mypage/delivery.twig")
91
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
92 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...
93
    {
94 1
        $Customer = $app['user'];
95
96
        return [
97 1
            'Customer' => $Customer,
98
        ];
99
    }
100
101
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
102
     * お届け先編集画面.
103
     *
104
     * @Route("/mypage/delivery/new", name="mypage_delivery_new")
105
     * @Route("/mypage/delivery/{id}/edit", name="mypage_delivery_edit", requirements={"id" = "\d+"})
106
     * @Template("Mypage/delivery_edit.twig")
107
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
108 4
    public function edit(Application $app, Request $request, $id = null)
109
    {
110 4
        $Customer = $app['user'];
111
112
        // 配送先住所最大値判定
113
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
114 4 View Code Duplication
        if (is_null($id)) {
115 2
            $addressCurrNum = count($Customer->getCustomerAddresses());
116 2
            $addressMax = $this->appConfig['deliv_addr_max'];
117 2
            if ($addressCurrNum >= $addressMax) {
118
                throw new NotFoundHttpException('お届け先の登録数の上限を超えています');
119
            }
120
        }
121
122 4
        $CustomerAddress = $this->customerAddressRepository->findOrCreateByCustomerAndId($Customer, $id);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Repository\Custom...CreateByCustomerAndId() has been deprecated with message: 呼び出し元で制御する

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
123
124 4
        $parentPage = $request->get('parent_page', null);
125
126
        // 正しい遷移かをチェック
127
        $allowdParents = array(
128 4
            $app->url('mypage_delivery'),
129 4
            $app->url('shopping_redirect_to'),
130
        );
131
132
        // 遷移が正しくない場合、デフォルトであるマイページの配送先追加の画面を設定する
133 4
        if (!in_array($parentPage, $allowdParents)) {
134
            // @deprecated 使用されていないコード
135 4
            $parentPage = $app->url('mypage_delivery');
136
        }
137
138 4
        $builder = $this->formFactory
139 4
            ->createBuilder(CustomerAddressType::class, $CustomerAddress);
140
141 4
        $event = new EventArgs(
142
            array(
143 4
                'builder' => $builder,
144 4
                'Customer' => $Customer,
145 4
                'CustomerAddress' => $CustomerAddress,
146
            ),
147 4
            $request
148
        );
149 4
        $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_INITIALIZE, $event);
150
151 4
        $form = $builder->getForm();
152 4
        $form->handleRequest($request);
153
154 4
        if ($form->isSubmitted() && $form->isValid()) {
155 2
            log_info('お届け先登録開始', array($id));
156
157 2
            $this->entityManager->persist($CustomerAddress);
158 2
            $this->entityManager->flush();
159
160 2
            log_info('お届け先登録完了', array($id));
161
162 2
            $event = new EventArgs(
163
                array(
164 2
                    'form' => $form,
165 2
                    'Customer' => $Customer,
166 2
                    'CustomerAddress' => $CustomerAddress,
167
                ),
168 2
                $request
169
            );
170 2
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_EDIT_COMPLETE, $event);
171
172 2
            $app->addSuccess('mypage.delivery.add.complete');
173
174 2
            return $app->redirect($app->url('mypage_delivery'));
175
        }
176
177
        return [
178 2
            'form' => $form->createView(),
179 2
            'parentPage' => $parentPage,
180 2
            'BaseInfo' => $this->BaseInfo,
181
        ];
182
    }
183
184
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
185
     * お届け先を削除する.
186
     *
187
     * @Route("/mypage/delivery/{id}/delete", name="mypage_delivery_delete")
188
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
189 2
    public function delete(Application $app, Request $request, $id)
190
    {
191 2
        $this->isTokenValid($app);
192
193 2
        log_info('お届け先削除開始', array($id));
194
195 2
        $Customer = $app['user'];
196
197 2
        $status = $this->customerAddressRepository->deleteByCustomerAndId($Customer, $id);
198
199 2 View Code Duplication
        if ($status) {
200 1
            $event = new EventArgs(
201
                array(
202 1
                    'id' => $id,
203 1
                    'Customer' => $Customer,
204 1
                ), $request
205
            );
206 1
            $this->eventDispatcher->dispatch(EccubeEvents::FRONT_MYPAGE_DELIVERY_DELETE_COMPLETE, $event);
207
208 1
            $app->addSuccess('mypage.address.delete.complete');
209
210 1
            log_info('お届け先削除完了', array($id));
211
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
212
        } else {
213 1
            $app->addError('mypage.address.delete.failed');
214
215 1
            log_info('お届け先削除失敗', array($id));
216
        }
217
218 2
        return $app->redirect($app->url('mypage_delivery'));
219
    }
220
}
221