Failed Conditions
Push — experimental/3.1 ( ea436b...26b8cd )
by chihiro
244:59 queued 239:01
created

CustomerDeliveryEditController::edit()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 73
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 45
c 2
b 0
f 0
nc 8
nop 4
dl 0
loc 73
rs 6.6896

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
namespace Eccube\Controller\Admin\Customer;
25
26
use Doctrine\ORM\EntityManager;
27
use Eccube\Annotation\Inject;
28
use Eccube\Application;
29
use Eccube\Common\Constant;
30
use Eccube\Controller\AbstractController;
31
use Eccube\Entity\CustomerAddress;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Admin\CustomerType;
35
use Eccube\Repository\CustomerRepository;
36
use Eccube\Repository\CustomerAddressRepository;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
40
use Symfony\Component\EventDispatcher\EventDispatcher;
41
use Symfony\Component\Form\FormFactory;
42
use Symfony\Component\HttpFoundation\Request;
43
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
44
use Eccube\Form\Type\Front\CustomerAddressType;
45
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
46
use Eccube\Entity\Customer;
47
48
/**
49
 * @Route(service=CustomerDeliveryEditController::class)
50
 */
51
class CustomerDeliveryEditController extends AbstractController
52
{
53
    /**
54
     * @Inject("eccube.event.dispatcher")
55
     * @var EventDispatcher
56
     */
57
    protected $eventDispatcher;
58
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
59
    /**
60
     * @Inject("form.factory")
61
     * @var FormFactory
62
     */
63
    protected $formFactory;
64
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
65
    /**
66
     * @Inject("config")
67
     * @var array
68
     */
69
    protected $appConfig;
70
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
71
    /**
72
     * @Inject("orm.em")
73
     * @var EntityManager
74
     */
75
    protected $entityManager;
76
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
77
    /**
78
     * @Inject(CustomerRepository::class)
79
     * @var CustomerRepository
80
     */
81
    protected $customerRepository;
82
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
83
    /**
84
     * @Inject(CustomerAddressRepository::class)
85
     * @var CustomerAddressRepository
86
     */
87
    protected $customerAddressRepository;
88
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
89
    /**
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 "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$did" missing
Loading history...
90
     * お届け先編集画面.
91
     *
92
     * @Route("/{_admin}/customer/{id}/delivery/new", name="admin_customer_delivery_new", requirements={"id" = "\d+"})
93
     * @Route("/{_admin}/customer/{id}/delivery/{did}/edit", name="admin_customer_delivery_edit", requirements={"id" = "\d+", "did" = "\d+"})
94
     * @Template("Customer/delivery_edit.twig")
95
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
96
    public function edit(Application $app, Request $request, Customer $Customer, $did = null)
97
    {   
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
98
        // 配送先住所最大値判定
99
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
100
        if (is_null($did)) {
101
            $addressCurrNum = count($Customer->getCustomerAddresses());
102
            $addressMax = $this->appConfig['deliv_addr_max'];
103
            if ($addressCurrNum >= $addressMax) {
104
                throw new NotFoundHttpException('お届け先の登録数の上限を超えています');
105
            }
106
        } else {
107
            $CustomerAddress = $this->customerAddressRepository->find($did);
108
            if (is_null($CustomerAddress) || $CustomerAddress->getCustomer()->getId() != $Customer->getId()) {
109
                throw new NotFoundHttpException();
110
            }
111
        }
112
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
113
        $CustomerAddress = $this->customerAddressRepository->findOrCreateByCustomerAndId($Customer, $did);
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...
114
115
        $builder = $this->formFactory
116
            ->createBuilder(CustomerAddressType::class, $CustomerAddress);
117
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
118
        $event = new EventArgs(
119
            array(
120
                'builder' => $builder,
121
                'Customer' => $Customer,
122
                'CustomerAddress' => $CustomerAddress,
123
            ),
124
            $request
125
        );
126
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
127
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_EDIT_INDEX_INITIALIZE, $event);
128
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
129
        $form = $builder->getForm();
130
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
131
        if ('POST' === $request->getMethod()) {
132
            $form->handleRequest($request);
133
            if ($form->isValid()) {
134
                log_info('お届け先登録開始', array($did));
135
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
136
                $this->entityManager->persist($CustomerAddress);
137
                $this->entityManager->flush();
138
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
139
                log_info('お届け先登録完了', array($did));
140
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
141
                $event = new EventArgs(
142
                    array(
143
                        'form' => $form,
144
                        'Customer' => $Customer,
145
                        'CustomerAddress' => $CustomerAddress,
146
                    ),
147
                    $request
148
                    );
149
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_EDIT_INDEX_COMPLETE, $event);
150
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
151
                $app->addSuccess('admin.customer.delivery.save.complete', 'admin');
152
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
153
                return $app->redirect($app->url('admin_customer_delivery_edit', array(
154
                    'id' => $Customer->getId(),
155
                    'did' => $CustomerAddress->getId(),
156
                )));
157
            } else {
158
                $app->addError('admin.customer.delivery.save.failed', 'admin');
159
            }
160
        }
161
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
162
        return [
163
            'form' => $form->createView(),
164
            'Customer' => $Customer,
165
            'CustomerAddress' => $CustomerAddress,
166
        ];
167
                    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
168
    }
169
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
170
    /**
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 "$Customer" missing
Loading history...
introduced by
Doc comment for parameter "$did" missing
Loading history...
171
     * @Method("DELETE")
172
     * @Route("/{_admin}/customer/{id}/delivery/{did}/delete", requirements={"id" = "\d+", "did" = "\d+"}, name="admin_customer_delivery_delete")
173
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
174
    public function delete(Application $app, Request $request, Customer $Customer, $did)
175
    {
176
        $this->isTokenValid($app);
177
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
178
        log_info('お届け先削除開始', array($did));
179
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
180
        $CustomerAddress = $this->customerAddressRepository->find($did);
181
        if (is_null($CustomerAddress)) {
182
            throw new NotFoundHttpException();
183
        } else if ($CustomerAddress->getCustomer()->getId() != $Customer->getId()) {
184
            $app->deleteMessage();
185
            return $app->redirect($app->url('admin_customer_edit', array('id' => $Customer->getId())));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
186
        }
187
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
188
        try {
189
            $this->customerAddressRepository->delete($CustomerAddress);
190
            $app->addSuccess('admin.customer.delivery.delete.complete', 'admin');
191
        } catch (ForeignKeyConstraintViolationException $e) {
192
            log_error('お届け先削除失敗', [$e], 'admin');
0 ignored issues
show
Unused Code introduced by
The call to log_error() has too many arguments starting with 'admin'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
193
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
194
            $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => 'お届け先']);
195
            $app->addError($message, 'admin');
196
        }
197
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
198
        log_info('お届け先削除完了', array($did));
199
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
200
        $event = new EventArgs(
201
            array(
202
                'Customer' => $Customer,
203
                'CustomerAddress' => $CustomerAddress,
204
            ),
205
            $request
206
        );
207
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_DELETE_COMPLETE, $event);
208
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
209
        return $app->redirect($app->url('admin_customer_edit', array('id' => $Customer->getId())));
210
    }
211
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
212
}
213