Failed Conditions
Push — experimental/sf ( 267a6e...28d741 )
by Ryo
1408:20 queued 1400:20
created

PaymentController::edit()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 7.0206

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 2
dl 0
loc 74
ccs 37
cts 40
cp 0.925
crap 7.0206
rs 7.6339
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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Setting\Shop;
15
16
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
17
use Eccube\Controller\AbstractController;
18
use Eccube\Entity\Payment;
19
use Eccube\Event\EccubeEvents;
20
use Eccube\Event\EventArgs;
21
use Eccube\Form\Type\Admin\PaymentRegisterType;
22
use Eccube\Repository\PaymentRepository;
23
use Eccube\Service\Payment\Method\Cash;
24
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
27
use Symfony\Component\Filesystem\Filesystem;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
30
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
31
use Symfony\Component\HttpFoundation\Response;
32
33
/**
34
 * Class PaymentController
35
 */
36
class PaymentController extends AbstractController
37
{
38
    /**
39
     * @var PaymentRepository
40
     */
41
    protected $paymentRepository;
42
43
    /**
44
     * PaymentController constructor.
45
     *
46
     * @param PaymentRepository $paymentRepository
47
     */
48 14
    public function __construct(PaymentRepository $paymentRepository)
49
    {
50 14
        $this->paymentRepository = $paymentRepository;
51
    }
52
53
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
54
     * @Route("/%eccube_admin_route%/setting/shop/payment", name="admin_setting_shop_payment")
55
     * @Template("@admin/Setting/Shop/payment.twig")
56
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
57 1
    public function index(Request $request)
58
    {
59 1
        $Payments = $this->paymentRepository
60 1
            ->findBy(
61 1
                [],
62 1
                ['sort_no' => 'DESC']
63
            );
64
65 1
        $event = new EventArgs(
66
            [
67 1
                'Payments' => $Payments,
68
            ],
69 1
            $request
70
        );
71 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_INDEX_COMPLETE, $event);
72
73
        return [
74 1
            'Payments' => $Payments,
75
        ];
76
    }
77
78
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Payment" missing
Loading history...
79
     * @Route("/%eccube_admin_route%/setting/shop/payment/new", name="admin_setting_shop_payment_new")
80
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_edit")
81
     * @Template("@admin/Setting/Shop/payment_edit.twig")
82
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
83 6
    public function edit(Request $request, Payment $Payment = null)
84
    {
85 6
        if (is_null($Payment)) {
86
            // FIXME
87 3
            $Payment = $this->paymentRepository
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Repository\Paymen...ository::findOrCreate() 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...
88 3
                ->findOrCreate(0);
89
        }
90
91 6
        $builder = $this->formFactory
92 6
            ->createBuilder(PaymentRegisterType::class, $Payment);
93
94 6
        $event = new EventArgs(
95
            [
96 6
                'builder' => $builder,
97 6
                'Payment' => $Payment,
98
            ],
99 6
            $request
100
        );
101 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_INITIALIZE, $event);
102
103 6
        $form = $builder->getForm();
104
105 6
        $form->setData($Payment);
106 6
        $form->handleRequest($request);
107
108
        // 既に画像保存されてる場合は取得する
109 6
        $oldPaymentImage = $Payment->getPaymentImage();
110
111
        // 登録ボタン押下
112 6
        if ($form->isSubmitted()) {
113 4
            if ($form->isValid()) {
114 2
                $Payment = $form->getData();
115
116
                // ファイルアップロード
117 2
                $file = $form['payment_image']->getData();
118 2
                $fs = new Filesystem();
119 2
                if ($file && $fs->exists($this->getParameter('eccube_temp_image_dir').'/'.$file)) {
120
                    $fs->rename(
121
                        $this->getParameter('eccube_temp_image_dir').'/'.$file,
122
                        $this->getParameter('eccube_save_image_dir').'/'.$file
123
                    );
124
                }
125
126
                // Payment method class of Cash to default.
127 2
                if (!$Payment->getMethodClass()) {
128 1
                    $Payment->setMethodClass(Cash::class);
129
                }
130 2
                $this->entityManager->persist($Payment);
131 2
                $this->entityManager->flush();
132
133 2
                $event = new EventArgs(
134
                    [
135 2
                        'form' => $form,
136 2
                        'Payment' => $Payment,
137
                    ],
138 2
                    $request
139
                );
140 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_COMPLETE, $event);
141
142 2
                $this->addSuccess('admin.register.complete', 'admin');
143
144 2
                return $this->redirectToRoute('admin_setting_shop_payment');
145
            } else {
146 2
                $this->addError('admin.register.failed', 'admin');
147
            }
148
        }
149
150
        return [
151 4
            'form' => $form->createView(),
152 4
            'payment_id' => $Payment->getId(),
153 4
            'Payment' => $Payment,
154 4
            'oldPaymentImage' => $oldPaymentImage,
155
        ];
156
    }
157
158
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
159
     * @Route("/%eccube_admin_route%/setting/shop/payment/image/add", name="admin_payment_image_add")
160
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
161 2
    public function imageAdd(Request $request)
162
    {
163 2
        if (!$request->isXmlHttpRequest()) {
164 1
            throw new BadRequestHttpException();
165
        }
166
167 1
        $images = $request->files->get('payment_register');
168 1
        $filename = null;
169 1
        if (isset($images['payment_image_file'])) {
170
            $image = $images['payment_image_file'];
171
172
            //ファイルフォーマット検証
173
            $mimeType = $image->getMimeType();
174
            if (0 !== strpos($mimeType, 'image')) {
175
                throw new UnsupportedMediaTypeHttpException();
176
            }
177
178
            $extension = $image->guessExtension();
179
            $filename = date('mdHis').uniqid('_').'.'.$extension;
180
            $image->move($this->getParameter('eccube_temp_image_dir'), $filename);
181
        }
182 1
        $event = new EventArgs(
183
            [
184 1
                'images' => $images,
185 1
                'filename' => $filename,
186
            ],
187 1
            $request
188
        );
189 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_IMAGE_ADD_COMPLETE, $event);
190 1
        $filename = $event->getArgument('filename');
191
192 1
        return $this->json(['filename' => $filename], 200);
193
    }
194
195
    /**
196
     * @Method("DELETE")
197
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_delete")
198
     *
199
     * @param Request $request
200
     * @param Payment $TargetPayment
201
     *
202
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
203
     */
204 1
    public function delete(Request $request, Payment $TargetPayment)
205
    {
206 1
        $this->isTokenValid();
207
208 1
        $sortNo = 1;
209 1
        $Payments = $this->paymentRepository->findBy([], ['sort_no' => 'ASC']);
210 1
        foreach ($Payments as $Payment) {
211 1
            $Payment->setSortNo($sortNo++);
212
        }
213
214
        try {
215 1
            $this->paymentRepository->delete($TargetPayment);
216 1
            $this->entityManager->flush();
217
218 1
            $event = new EventArgs(
219
                [
220 1
                    'Payment' => $TargetPayment,
221
                ],
222 1
                $request
223
            );
224 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_DELETE_COMPLETE, $event);
225
226 1
            $this->addSuccess('admin.delete.complete', 'admin');
227
        } catch (ForeignKeyConstraintViolationException $e) {
228
            $this->entityManager->rollback();
229
230
            $message = trans('admin.delete.failed.foreign_key', ['%name%' => trans('payment.text.name')]);
231
            $this->addError($message, 'admin');
232
        }
233
234 1
        return $this->redirectToRoute('admin_setting_shop_payment');
235
    }
236
237
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$current" missing
Loading history...
238
     * @Method("PUT")
239
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/up", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_up")
240
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
241 1 View Code Duplication
    public function up(Payment $current)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
242
    {
243 1
        $this->isTokenValid();
244
245 1
        $currentSortNo = $current->getSortNo();
246 1
        $targetSortNo = $currentSortNo + 1;
247
248 1
        $target = $this->paymentRepository->findOneBy(['sort_no' => $targetSortNo]);
249
250 1
        if ($target) {
251 1
            $this->entityManager->persist($target->setSortNo($currentSortNo));
252 1
            $this->entityManager->persist($current->setSortNo($targetSortNo));
253 1
            $this->entityManager->flush();
254
255 1
            $this->addSuccess('admin.sort_no.move.complete', 'admin');
256
        } else {
257
            $this->addError('admin.sort_no.up.error', 'admin');
258
        }
259
260 1
        return $this->redirectToRoute('admin_setting_shop_payment');
261
    }
262
263
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$current" missing
Loading history...
264
     * @Method("PUT")
265
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/down", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_down")
266
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
267 1 View Code Duplication
    public function down(Payment $current)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
268
    {
269 1
        $this->isTokenValid();
270
271 1
        $currentSortNo = $current->getSortNo();
272 1
        $targetSortNo = $currentSortNo - 1;
273
274 1
        $target = $this->paymentRepository->findOneBy(['sort_no' => $targetSortNo]);
275
276 1
        if ($target) {
277 1
            $this->entityManager->persist($target->setSortNo($currentSortNo));
278 1
            $this->entityManager->persist($current->setSortNo($targetSortNo));
279 1
            $this->entityManager->flush();
280
281 1
            $this->addSuccess('admin.sort_no.move.complete', 'admin');
282
        } else {
283
            $this->addError('admin.sort_no.down.error', 'admin');
284
        }
285
286 1
        return $this->redirectToRoute('admin_setting_shop_payment');
287
    }
288
289
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Payment" missing
Loading history...
290
     * @Method("PUT")
291
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/visible", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_visible")
292
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
293
    public function visible(Payment $Payment)
294
    {
295
        $this->isTokenValid();
296
297
        $Payment->setVisible(!$Payment->isVisible());
298
299
        $this->entityManager->flush();
300
301
        if ($Payment->isVisible()) {
302
            $this->addSuccess('admin.payment.visible.complete', 'admin');
303
        } else {
304
            $this->addSuccess('admin.payment.invisible.complete', 'admin');
305
        }
306
307
        return $this->redirectToRoute('admin_setting_shop_payment');
308
    }
309
310
    /**
311
     * @Method("POST")
312
     * @Route("/%eccube_admin_route%/setting/shop/payment/sort_no/move", name="admin_setting_shop_payment_sort_no_move")
313
     *
314
     * @param Request $request
315
     *
316
     * @return Response
317
     */
318 1 View Code Duplication
    public function moveSortNo(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
319
    {
320 1
        if ($request->isXmlHttpRequest()) {
321 1
            $this->isTokenValid();
322 1
            $sortNos = $request->request->all();
323 1
            foreach ($sortNos as $paymentId => $sortNo) {
324
                /** @var Payment $Payment */
325 1
                $Payment = $this->paymentRepository
326 1
                    ->find($paymentId);
327 1
                $Payment->setSortNo($sortNo);
328 1
                $this->entityManager->persist($Payment);
329
            }
330 1
            $this->entityManager->flush();
331
        }
332
333 1
        return new Response();
334
    }
335
}
336