Failed Conditions
Pull Request — experimental/sf (#3240)
by Kentaro
43:45
created

PaymentController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 298
Duplicated Lines 19.8 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 81.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 59
loc 298
ccs 109
cts 133
cp 0.8195
rs 10
c 1
b 0
f 0
wmc 24
lcom 1
cbo 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 20 1
B edit() 0 72 6
A imageAdd() 0 33 4
A delete() 0 32 3
A up() 21 21 2
A down() 21 21 2
A visible() 0 16 2
A moveSortNo() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 2
                $Payment->setMethodClass(Cash::class); // Payment method class of Cash to default.
127 2
                $Payment->setVisible(true);
128 2
                $this->entityManager->persist($Payment);
129 2
                $this->entityManager->flush();
130
131 2
                $event = new EventArgs(
132
                    [
133 2
                        'form' => $form,
134 2
                        'Payment' => $Payment,
135
                    ],
136 2
                    $request
137
                );
138 2
                $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_COMPLETE, $event);
139
140 2
                $this->addSuccess('admin.register.complete', 'admin');
141
142 2
                return $this->redirectToRoute('admin_setting_shop_payment');
143
            } else {
144 2
                $this->addError('admin.register.failed', 'admin');
145
            }
146
        }
147
148
        return [
149 4
            'form' => $form->createView(),
150 4
            'payment_id' => $Payment->getId(),
151 4
            'Payment' => $Payment,
152 4
            'oldPaymentImage' => $oldPaymentImage,
153
        ];
154
    }
155
156
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
157
     * @Route("/%eccube_admin_route%/setting/shop/payment/image/add", name="admin_payment_image_add")
158
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
159 2
    public function imageAdd(Request $request)
160
    {
161 2
        if (!$request->isXmlHttpRequest()) {
162 1
            throw new BadRequestHttpException();
163
        }
164
165 1
        $images = $request->files->get('payment_register');
166 1
        $filename = null;
167 1
        if (isset($images['payment_image_file'])) {
168
            $image = $images['payment_image_file'];
169
170
            //ファイルフォーマット検証
171
            $mimeType = $image->getMimeType();
172
            if (0 !== strpos($mimeType, 'image')) {
173
                throw new UnsupportedMediaTypeHttpException();
174
            }
175
176
            $extension = $image->guessExtension();
177
            $filename = date('mdHis').uniqid('_').'.'.$extension;
178
            $image->move($this->getParameter('eccube_temp_image_dir'), $filename);
179
        }
180 1
        $event = new EventArgs(
181
            [
182 1
                'images' => $images,
183 1
                'filename' => $filename,
184
            ],
185 1
            $request
186
        );
187 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_IMAGE_ADD_COMPLETE, $event);
188 1
        $filename = $event->getArgument('filename');
189
190 1
        return $this->json(['filename' => $filename], 200);
191
    }
192
193
    /**
194
     * @Method("DELETE")
195
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_delete")
196
     *
197
     * @param Request $request
198
     * @param Payment $TargetPayment
199
     *
200
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
201
     */
202 1
    public function delete(Request $request, Payment $TargetPayment)
203
    {
204 1
        $this->isTokenValid();
205
206 1
        $sortNo = 1;
207 1
        $Payments = $this->paymentRepository->findBy([], ['sort_no' => 'ASC']);
208 1
        foreach ($Payments as $Payment) {
209 1
            $Payment->setSortNo($sortNo++);
210
        }
211
212
        try {
213 1
            $this->paymentRepository->delete($TargetPayment);
214 1
            $this->entityManager->flush();
215
216 1
            $event = new EventArgs(
217
                [
218 1
                    'Payment' => $TargetPayment,
219
                ],
220 1
                $request
221
            );
222 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_DELETE_COMPLETE, $event);
223
224 1
            $this->addSuccess('admin.delete.complete', 'admin');
225
        } catch (ForeignKeyConstraintViolationException $e) {
226
            $this->entityManager->rollback();
227
228
            $message = trans('admin.delete.failed.foreign_key', ['%name%' => trans('payment.text.name')]);
229
            $this->addError($message, 'admin');
230
        }
231
232 1
        return $this->redirectToRoute('admin_setting_shop_payment');
233
    }
234
235
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$current" missing
Loading history...
236
     * @Method("PUT")
237
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/up", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_up")
238
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
239 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...
240
    {
241 1
        $this->isTokenValid();
242
243 1
        $currentSortNo = $current->getSortNo();
244 1
        $targetSortNo = $currentSortNo + 1;
245
246 1
        $target = $this->paymentRepository->findOneBy(['sort_no' => $targetSortNo]);
247
248 1
        if ($target) {
249 1
            $this->entityManager->persist($target->setSortNo($currentSortNo));
250 1
            $this->entityManager->persist($current->setSortNo($targetSortNo));
251 1
            $this->entityManager->flush();
252
253 1
            $this->addSuccess('admin.sort_no.move.complete', 'admin');
254
        } else {
255
            $this->addError('admin.sort_no.up.error', 'admin');
256
        }
257
258 1
        return $this->redirectToRoute('admin_setting_shop_payment');
259
    }
260
261
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$current" missing
Loading history...
262
     * @Method("PUT")
263
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/down", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_down")
264
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
265 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...
266
    {
267 1
        $this->isTokenValid();
268
269 1
        $currentSortNo = $current->getSortNo();
270 1
        $targetSortNo = $currentSortNo - 1;
271
272 1
        $target = $this->paymentRepository->findOneBy(['sort_no' => $targetSortNo]);
273
274 1
        if ($target) {
275 1
            $this->entityManager->persist($target->setSortNo($currentSortNo));
276 1
            $this->entityManager->persist($current->setSortNo($targetSortNo));
277 1
            $this->entityManager->flush();
278
279 1
            $this->addSuccess('admin.sort_no.move.complete', 'admin');
280
        } else {
281
            $this->addError('admin.sort_no.down.error', 'admin');
282
        }
283
284 1
        return $this->redirectToRoute('admin_setting_shop_payment');
285
    }
286
287
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Payment" missing
Loading history...
288
     * @Method("PUT")
289
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/visible", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_visible")
290
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
291
    public function visible(Payment $Payment)
292
    {
293
        $this->isTokenValid();
294
295
        $Payment->setVisible(!$Payment->isVisible());
296
297
        $this->entityManager->flush();
298
299
        if ($Payment->isVisible()) {
300
            $this->addSuccess('admin.payment.visible.complete', 'admin');
301
        } else {
302
            $this->addSuccess('admin.payment.invisible.complete', 'admin');
303
        }
304
305
        return $this->redirectToRoute('admin_setting_shop_payment');
306
    }
307
308
    /**
309
     * @Method("POST")
310
     * @Route("/%eccube_admin_route%/setting/shop/payment/sort_no/move", name="admin_setting_shop_payment_sort_no_move")
311
     *
312
     * @param Request $request
313
     *
314
     * @return Response
315
     */
316 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...
317
    {
318 1
        if ($request->isXmlHttpRequest()) {
319 1
            $this->isTokenValid();
320 1
            $sortNos = $request->request->all();
321 1
            foreach ($sortNos as $paymentId => $sortNo) {
322
                /** @var Payment $Payment */
323 1
                $Payment = $this->paymentRepository
324 1
                    ->find($paymentId);
325 1
                $Payment->setSortNo($sortNo);
326 1
                $this->entityManager->persist($Payment);
327
            }
328 1
            $this->entityManager->flush();
329
        }
330
331 1
        return new Response();
332
    }
333
}
334