Failed Conditions
Pull Request — experimental/sf (#3240)
by Kentaro
241:36 queued 231:42
created

PaymentController::imageAdd()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.686

Importance

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