Failed Conditions
Pull Request — 4.0 (#3593)
by chihiro
08:59
created

PaymentController::down()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 21
loc 21
ccs 10
cts 11
cp 0.9091
crap 2.003
rs 9.584
c 0
b 0
f 0
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\Template;
25
use Symfony\Component\Filesystem\Filesystem;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
29
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
30
use Symfony\Component\Routing\Annotation\Route;
31
32
/**
33
 * Class PaymentController
34
 */
35
class PaymentController extends AbstractController
36
{
37
    /**
38
     * @var PaymentRepository
39
     */
40
    protected $paymentRepository;
41
42
    /**
43
     * PaymentController constructor.
44
     *
45
     * @param PaymentRepository $paymentRepository
46
     */
47
    public function __construct(PaymentRepository $paymentRepository)
48 14
    {
49
        $this->paymentRepository = $paymentRepository;
50 14
    }
51
52
    /**
53
     * @Route("/%eccube_admin_route%/setting/shop/payment", name="admin_setting_shop_payment")
54
     * @Template("@admin/Setting/Shop/payment.twig")
55
     */
56 View Code Duplication
    public function index(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...
57 1
    {
58
        $Payments = $this->paymentRepository
59 1
            ->findBy(
60 1
                [],
61 1
                ['sort_no' => 'DESC']
62 1
            );
63
64
        $event = new EventArgs(
65 1
            [
66
                'Payments' => $Payments,
67 1
            ],
68
            $request
69 1
        );
70
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_INDEX_COMPLETE, $event);
71 1
72
        return [
73
            'Payments' => $Payments,
74 1
        ];
75
    }
76
77
    /**
78
     * @Route("/%eccube_admin_route%/setting/shop/payment/new", name="admin_setting_shop_payment_new")
79
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_edit")
80
     * @Template("@admin/Setting/Shop/payment_edit.twig")
81
     */
82
    public function edit(Request $request, Payment $Payment = null)
83 6
    {
84
        if (is_null($Payment)) {
85 6
            // FIXME
86
            $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...
87 3
                ->findOrCreate(0);
88 3
        }
89
90
        $builder = $this->formFactory
91 6
            ->createBuilder(PaymentRegisterType::class, $Payment);
92 6
93
        $event = new EventArgs(
94 6
            [
95
                'builder' => $builder,
96 6
                'Payment' => $Payment,
97 6
            ],
98
            $request
99 6
        );
100
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_INITIALIZE, $event);
101 6
102
        $form = $builder->getForm();
103 6
104
        $form->setData($Payment);
105 6
        $form->handleRequest($request);
106 6
107
        // 既に画像保存されてる場合は取得する
108
        $oldPaymentImage = $Payment->getPaymentImage();
109 6
110
        // 登録ボタン押下
111
        if ($form->isSubmitted() && $form->isValid()) {
112 6
            $Payment = $form->getData();
113 4
114 2
            // ファイルアップロード
115
            $file = $form['payment_image']->getData();
116
            $fs = new Filesystem();
117 2
            if ($file && $fs->exists($this->getParameter('eccube_temp_image_dir').'/'.$file)) {
118 2
                $fs->rename(
119 2
                    $this->getParameter('eccube_temp_image_dir').'/'.$file,
120
                    $this->getParameter('eccube_save_image_dir').'/'.$file
121
                );
122
            }
123
124
            // Payment method class of Cash to default.
125
            if (!$Payment->getMethodClass()) {
126
                $Payment->setMethodClass(Cash::class);
127 2
            }
128 1
            $this->entityManager->persist($Payment);
129
            $this->entityManager->flush();
130 2
131 2
            $event = new EventArgs(
132
                [
133 2
                    'form' => $form,
134
                    'Payment' => $Payment,
135 2
                ],
136 2
                $request
137
            );
138 2
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_COMPLETE, $event);
139
140 2
            $this->addSuccess('admin.common.save_complete', 'admin');
141
142 2
            return $this->redirectToRoute('admin_setting_shop_payment_edit', ['id' => $Payment->getId()]);
143
        }
144 2
145
        return [
146 2
            'form' => $form->createView(),
147
            'payment_id' => $Payment->getId(),
148
            'Payment' => $Payment,
149
            'oldPaymentImage' => $oldPaymentImage,
150
        ];
151 4
    }
152 4
153 4
    /**
154 4
     * @Route("/%eccube_admin_route%/setting/shop/payment/image/add", name="admin_payment_image_add")
155
     */
156
    public function imageAdd(Request $request)
157
    {
158
        if (!$request->isXmlHttpRequest()) {
159
            throw new BadRequestHttpException();
160
        }
161 2
162
        $images = $request->files->get('payment_register');
163 2
        $filename = null;
164 1
        if (isset($images['payment_image_file'])) {
165
            $image = $images['payment_image_file'];
166
167 1
            //ファイルフォーマット検証
168 1
            $mimeType = $image->getMimeType();
169 1
            if (0 !== strpos($mimeType, 'image')) {
170
                throw new UnsupportedMediaTypeHttpException();
171
            }
172
173
            $extension = $image->guessExtension();
174
            $filename = date('mdHis').uniqid('_').'.'.$extension;
175
            $image->move($this->getParameter('eccube_temp_image_dir'), $filename);
176
        }
177
        $event = new EventArgs(
178
            [
179
                'images' => $images,
180
                'filename' => $filename,
181
            ],
182 1
            $request
183
        );
184 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_IMAGE_ADD_COMPLETE, $event);
185 1
        $filename = $event->getArgument('filename');
186
187 1
        return $this->json(['filename' => $filename], 200);
188
    }
189 1
190 1
    /**
191
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_delete", methods={"DELETE"})
192 1
     *
193
     * @param Request $request
194
     * @param Payment $TargetPayment
195
     *
196
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
197
     */
198
    public function delete(Request $request, Payment $TargetPayment)
199
    {
200
        $this->isTokenValid();
201
202
        $sortNo = 1;
203
        $Payments = $this->paymentRepository->findBy([], ['sort_no' => 'ASC']);
204 1
        foreach ($Payments as $Payment) {
205
            $Payment->setSortNo($sortNo++);
206 1
        }
207
208 1
        try {
209 1
            $this->paymentRepository->delete($TargetPayment);
210 1
            $this->entityManager->flush();
211 1
212
            $event = new EventArgs(
213
                [
214
                    'Payment' => $TargetPayment,
215 1
                ],
216 1
                $request
217
            );
218 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_DELETE_COMPLETE, $event);
219
220 1
            $this->addSuccess('admin.delete_complete', 'admin');
221
        } catch (ForeignKeyConstraintViolationException $e) {
222 1
            $this->entityManager->rollback();
223
224 1
            $message = trans('admin.delete_error_foreign_key', ['%name%' => $TargetPayment->getMethod()]);
225
            $this->addError($message, 'admin');
226 1
        }
227
228
        return $this->redirectToRoute('admin_setting_shop_payment');
229
    }
230
231
    /**
232
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/visible", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_visible", methods={"PUT"})
233
     */
234 1
    public function visible(Payment $Payment)
235
    {
236
        $this->isTokenValid();
237
238
        $Payment->setVisible(!$Payment->isVisible());
239
240
        $this->entityManager->flush();
241 1
242
        if ($Payment->isVisible()) {
243 1
            $this->addSuccess('admin.payment.visible.complete', 'admin');
244
        } else {
245 1
            $this->addSuccess('admin.payment.invisible.complete', 'admin');
246 1
        }
247
248 1
        return $this->redirectToRoute('admin_setting_shop_payment');
249
    }
250 1
251 1
    /**
252 1
     * @Route("/%eccube_admin_route%/setting/shop/payment/sort_no/move", name="admin_setting_shop_payment_sort_no_move", methods={"POST"})
253 1
     *
254
     * @param Request $request
255 1
     *
256
     * @return Response
257
     */
258 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...
259
    {
260 1
        if (!$request->isXmlHttpRequest()) {
261
            throw new BadRequestHttpException();
262
        }
263
264
        if ($this->isTokenValid()) {
265
            $sortNos = $request->request->all();
266
            foreach ($sortNos as $paymentId => $sortNo) {
267 1
                /** @var Payment $Payment */
268
                $Payment = $this->paymentRepository
269 1
                    ->find($paymentId);
270
                $Payment->setSortNo($sortNo);
271 1
                $this->entityManager->persist($Payment);
272 1
            }
273
            $this->entityManager->flush();
274 1
275
            return new Response();
276 1
        }
277 1
    }
278
}
279