Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

Admin/Setting/Shop/PaymentController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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
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
            $Payment = $this->paymentRepository->findOneBy([], ['sort_no' => 'DESC']);
86
            $sortNo = 1;
87 3
            if ($Payment) {
88 3
                $sortNo = $Payment->getSortNo() + 1;
89
            }
90
91 6
            $Payment = new \Eccube\Entity\Payment();
92 6
            $Payment
93
                ->setSortNo($sortNo)
94 6
                ->setFixed(true)
95
                ->setVisible(true);
96 6
        }
97 6
98
        $builder = $this->formFactory
99 6
            ->createBuilder(PaymentRegisterType::class, $Payment);
100
101 6
        $event = new EventArgs(
102
            [
103 6
                'builder' => $builder,
104
                'Payment' => $Payment,
105 6
            ],
106 6
            $request
107
        );
108
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_INITIALIZE, $event);
109 6
110
        $form = $builder->getForm();
111
112 6
        // 既に画像保存されてる場合は取得する
113 4
        $oldPaymentImage = $Payment->getPaymentImage();
114 2
115
        $form->setData($Payment);
116
        $form->handleRequest($request);
117 2
118 2
        // 登録ボタン押下
119 2
        if ($form->isSubmitted() && $form->isValid()) {
120
            $Payment = $form->getData();
121
122
            // ファイルアップロード
123
            $file = $form['payment_image']->getData();
124
            $fs = new Filesystem();
125
            if ($file && $fs->exists($this->getParameter('eccube_temp_image_dir').'/'.$file)) {
126
                $fs->rename(
127 2
                    $this->getParameter('eccube_temp_image_dir').'/'.$file,
128 1
                    $this->getParameter('eccube_save_image_dir').'/'.$file
129
                );
130 2
            }
131 2
132
            // Payment method class of Cash to default.
133 2
            if (!$Payment->getMethodClass()) {
134
                $Payment->setMethodClass(Cash::class);
135 2
            }
136 2
            $this->entityManager->persist($Payment);
137
            $this->entityManager->flush();
138 2
139
            $event = new EventArgs(
140 2
                [
141
                    'form' => $form,
142 2
                    'Payment' => $Payment,
143
                ],
144 2
                $request
145
            );
146 2
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_COMPLETE, $event);
147
148
            $this->addSuccess('admin.common.save_complete', 'admin');
149
150
            return $this->redirectToRoute('admin_setting_shop_payment_edit', ['id' => $Payment->getId()]);
151 4
        }
152 4
153 4
        return [
154 4
            'form' => $form->createView(),
155
            'payment_id' => $Payment->getId(),
156
            'Payment' => $Payment,
157
            'oldPaymentImage' => $oldPaymentImage,
158
        ];
159
    }
160
161 2
    /**
162
     * @Route("/%eccube_admin_route%/setting/shop/payment/image/add", name="admin_payment_image_add")
163 2
     */
164 1
    public function imageAdd(Request $request)
165
    {
166
        if (!$request->isXmlHttpRequest()) {
167 1
            throw new BadRequestHttpException();
168 1
        }
169 1
170
        $images = $request->files->get('payment_register');
171
        $allowExtensions = ['gif', 'jpg', 'jpeg', 'png'];
172
        $filename = null;
173
        if (isset($images['payment_image_file'])) {
174
            $image = $images['payment_image_file'];
175
176
            //ファイルフォーマット検証
177
            $mimeType = $image->getMimeType();
178
            if (0 !== strpos($mimeType, 'image')) {
179
                throw new UnsupportedMediaTypeHttpException();
180
            }
181
182 1
            // 拡張子
183
            $extension = $image->getClientOriginalExtension();
184 1
            if (!in_array(strtolower($extension), $allowExtensions)) {
185 1
                throw new UnsupportedMediaTypeHttpException();
186
            }
187 1
188
            $filename = date('mdHis').uniqid('_').'.'.$extension;
189 1
            $image->move($this->getParameter('eccube_temp_image_dir'), $filename);
190 1
        }
191
        $event = new EventArgs(
192 1
            [
193
                'images' => $images,
194
                'filename' => $filename,
195
            ],
196
            $request
197
        );
198
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_IMAGE_ADD_COMPLETE, $event);
199
        $filename = $event->getArgument('filename');
200
201
        return $this->json(['filename' => $filename], 200);
202
    }
203
204 1
    /**
205
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_delete", methods={"DELETE"})
206 1
     *
207
     * @param Request $request
208 1
     * @param Payment $TargetPayment
209 1
     *
210 1
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
211 1
     */
212
    public function delete(Request $request, Payment $TargetPayment)
213
    {
214
        $this->isTokenValid();
215 1
216 1
        $sortNo = 1;
217
        $Payments = $this->paymentRepository->findBy([], ['sort_no' => 'ASC']);
218 1
        foreach ($Payments as $Payment) {
219
            $Payment->setSortNo($sortNo++);
220 1
        }
221
222 1
        try {
223
            $this->paymentRepository->delete($TargetPayment);
224 1
            $this->entityManager->flush();
225
226 1
            $event = new EventArgs(
227
                [
228
                    'Payment' => $TargetPayment,
229
                ],
230
                $request
231
            );
232
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_DELETE_COMPLETE, $event);
233
234 1
            $this->addSuccess('admin.common.delete_complete', 'admin');
235
        } catch (ForeignKeyConstraintViolationException $e) {
236
            $this->entityManager->rollback();
237
238
            $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $TargetPayment->getMethod()]);
239
            $this->addError($message, 'admin');
240
        }
241 1
242
        return $this->redirectToRoute('admin_setting_shop_payment');
243 1
    }
244
245 1
    /**
246 1
     * @Route("/%eccube_admin_route%/setting/shop/payment/{id}/visible", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_visible", methods={"PUT"})
247
     */
248 1
    public function visible(Payment $Payment)
249
    {
250 1
        $this->isTokenValid();
251 1
252 1
        $Payment->setVisible(!$Payment->isVisible());
253 1
254
        $this->entityManager->flush();
255 1
256 View Code Duplication
        if ($Payment->isVisible()) {
257
            $this->addSuccess(trans('admin.common.to_show_complete', ['%name%' => $Payment->getMethod()]), 'admin');
258
        } else {
259
            $this->addSuccess(trans('admin.common.to_hide_complete', ['%name%' => $Payment->getMethod()]), 'admin');
260 1
        }
261
262
        return $this->redirectToRoute('admin_setting_shop_payment');
263
    }
264
265
    /**
266
     * @Route("/%eccube_admin_route%/setting/shop/payment/sort_no/move", name="admin_setting_shop_payment_sort_no_move", methods={"POST"})
267 1
     *
268
     * @param Request $request
269 1
     *
270
     * @return Response
271 1
     */
272 1 View Code Duplication
    public function moveSortNo(Request $request)
273
    {
274 1
        if (!$request->isXmlHttpRequest()) {
275
            throw new BadRequestHttpException();
276 1
        }
277 1
278 1
        if ($this->isTokenValid()) {
279 1
            $sortNos = $request->request->all();
280
            foreach ($sortNos as $paymentId => $sortNo) {
281 1
                /** @var Payment $Payment */
282
                $Payment = $this->paymentRepository
283
                    ->find($paymentId);
284
                $Payment->setSortNo($sortNo);
285
                $this->entityManager->persist($Payment);
286 1
            }
287
            $this->entityManager->flush();
288
289
            return new Response();
290
        }
291
    }
292
}
293