Completed
Pull Request — experimental/3.1 (#2647)
by
unknown
38:47
created

CustomerDeliveryEditController::delete()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 27
nc 6
nop 4
dl 0
loc 44
rs 8.439
c 0
b 0
f 0
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 Symfony\Component\Security\Core\Encoder\EncoderFactory;
45
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
46
use Eccube\Form\Type\Front\CustomerAddressType;
47
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
48
49
/**
50
 * @Route(service=CustomerDeliveryEditController::class)
51
 */
52
class CustomerDeliveryEditController extends AbstractController
53
{
54
    /**
55
     * @Inject("eccube.event.dispatcher")
56
     * @var EventDispatcher
57
     */
58
    protected $eventDispatcher;
59
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
60
    /**
61
     * @Inject("form.factory")
62
     * @var FormFactory
63
     */
64
    protected $formFactory;
65
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
66
    /**
67
     * @Inject("config")
68
     * @var array
69
     */
70
    protected $appConfig;
71
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
72
    /**
73
     * @Inject("orm.em")
74
     * @var EntityManager
75
     */
76
    protected $entityManager;
77
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
78
    /**
79
     * @Inject(CustomerRepository::class)
80
     * @var CustomerRepository
81
     */
82
    protected $customerRepository;
83
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
84
    /**
85
     * @Inject(CustomerAddressRepository::class)
86
     * @var CustomerAddressRepository
87
     */
88
    protected $customerAddressRepository;
89
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
90
    /**
91
     * @Inject("security.encoder_factory")
92
     * @var EncoderFactoryInterface
93
     */
94
    protected $encoderFactory;
95
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
96
    /**
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...
introduced by
Doc comment for parameter "$did" missing
Loading history...
97
     * お届け先編集画面.
98
     *
99
     * @Route("/{_admin}/customer/{id}/delivery/new", name="admin_customer_delivery_new", requirements={"id" = "\d+"})
100
     * @Route("/{_admin}/customer/{id}/delivery/{did}/edit", name="admin_customer_delivery_edit", requirements={"id" = "\d+", "did" = "\d+"})
101
     * @Template("Customer/delivery_edit.twig")
102
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
103
    public function edit(Application $app, Request $request, $id, $did = null)
104
    {
105
        $Customer = $this->customerRepository->find($id);
106
        if (is_null($Customer)) {
107
            throw new NotFoundHttpException();
108
        }
109
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
110
        // 配送先住所最大値判定
111
        // $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ
112
        if (is_null($did)) {
113
            $addressCurrNum = count($Customer->getCustomerAddresses());
114
            $addressMax = $this->appConfig['deliv_addr_max'];
115
            if ($addressCurrNum >= $addressMax) {
116
                throw new NotFoundHttpException('お届け先の登録数の上限を超えています');
117
            }
118
        } else {
119
            $CustomerAddress = $this->customerAddressRepository->find($did);
120
            if (is_null($CustomerAddress) || $CustomerAddress->getCustomer()->getId() != $Customer->getId()) {
121
                throw new NotFoundHttpException();
122
            }
123
        }
124
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
125
        $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...
126
127
        $builder = $this->formFactory
128
            ->createBuilder(CustomerAddressType::class, $CustomerAddress);
129
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
130
        $event = new EventArgs(
131
            array(
132
                'builder' => $builder,
133
                'Customer' => $Customer,
134
                'CustomerAddress' => $CustomerAddress,
135
            ),
136
            $request
137
        );
138
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
139
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_EDIT_INDEX_INITIALIZE, $event);
140
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
141
        $form = $builder->getForm();
142
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
143
        if ('POST' === $request->getMethod()) {
144
            $form->handleRequest($request);
145
            if ($form->isValid()) {
146
                log_info('お届け先登録開始', array($did));
147
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
148
                $this->entityManager->persist($CustomerAddress);
149
                $this->entityManager->flush();
150
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
151
                log_info('お届け先登録完了', array($did));
152
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
153
                $event = new EventArgs(
154
                    array(
155
                        'form' => $form,
156
                        'Customer' => $Customer,
157
                        'CustomerAddress' => $CustomerAddress,
158
                    ),
159
                    $request
160
                    );
161
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_EDIT_INDEX_COMPLETE, $event);
162
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
163
                $app->addSuccess('admin.customer.delivery.save.complete', 'admin');
164
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
165
                return $app->redirect($app->url('admin_customer_delivery_edit', array(
166
                    'id' => $Customer->getId(),
167
                    'did' => $CustomerAddress->getId(),
168
                )));
169
            } else {
170
                $app->addError('admin.customer.delivery.save.failed', 'admin');
171
            }
172
        }
173
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
174
        return [
175
            'form' => $form->createView(),
176
            'Customer' => $Customer,
177
            'CustomerAddress' => $CustomerAddress,
178
        ];
179
                    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
180
    }
181
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
182
    /**
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...
introduced by
Doc comment for parameter "$did" missing
Loading history...
183
     * @Method("DELETE")
184
     * @Route("/{_admin}/customer/{id}/delivery/{did}/delete", requirements={"id" = "\d+", "did" = "\d+"}, name="admin_customer_delivery_delete")
185
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
186
    public function delete(Application $app, Request $request, $id, $did)
187
    {
188
        $this->isTokenValid($app);
189
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
190
        log_info('お届け先削除開始', array($did));
191
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
192
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
193
        $Customer = $this->customerRepository->find($id);
194
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
195
        if (is_null($Customer)) {
196
            throw new NotFoundHttpException();
197
        }
198
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
199
        $CustomerAddress = $this->customerAddressRepository->find($did);
200
        if (is_null($CustomerAddress)) {
201
            throw new NotFoundHttpException();
202
        } else if ($CustomerAddress->getCustomer()->getId() != $Customer->getId()) {
203
            $app->deleteMessage();
204
            return $app->redirect($app->url('admin_customer_edit', array('id' => $id)));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
205
        }
206
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
207
        try {
208
            $this->customerAddressRepository->delete($CustomerAddress);
209
            $app->addSuccess('admin.customer.delivery.delete.complete', 'admin');
210
        } catch (ForeignKeyConstraintViolationException $e) {
211
            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...
212
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
213
            $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => 'お届け先']);
214
            $app->addError($message, 'admin');
215
        }
216
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
217
        log_info('お届け先削除完了', array($id));
218
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
219
        $event = new EventArgs(
220
            array(
221
                'Customer' => $Customer,
222
                'CustomerAddress' => $CustomerAddress,
223
            ),
224
            $request
225
        );
226
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_DELETE_COMPLETE, $event);
227
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
228
        return $app->redirect($app->url('admin_customer_edit', array('id' => $id)));
229
    }
230
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
231
}
232