Failed Conditions
Pull Request — experimental/3.1 (#2512)
by chihiro
12:14
created

PaymentController::visible()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 16
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Controller\Admin\Setting\Shop;
26
27
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
28
use Doctrine\ORM\EntityManager;
29
use Eccube\Annotation\Component;
30
use Eccube\Annotation\Inject;
31
use Eccube\Application;
32
use Eccube\Controller\AbstractController;
33
use Eccube\Entity\Payment;
34
use Eccube\Event\EccubeEvents;
35
use Eccube\Event\EventArgs;
36
use Eccube\Form\Type\Admin\PaymentRegisterType;
37
use Eccube\Repository\PaymentRepository;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
39
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
40
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
41
use Symfony\Component\EventDispatcher\EventDispatcher;
42
use Symfony\Component\Filesystem\Filesystem;
43
use Symfony\Component\Form\FormFactory;
44
use Symfony\Component\HttpFoundation\Request;
45
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
46
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
47
48
/**
49
 * @Component
50
 * @Route(service=PaymentController::class)
51
 */
52
class PaymentController extends AbstractController
53
{
54
    /**
55
     * @Inject("orm.em")
56
     * @var EntityManager
57
     */
58
    protected $entityManager;
59
60
    /**
61
     * @Inject("config")
62
     * @var array
63
     */
64
    protected $appConfig;
65
66
    /**
67
     * @Inject("form.factory")
68
     * @var FormFactory
69
     */
70
    protected $formFactory;
71
72
    /**
73
     * @Inject("eccube.event.dispatcher")
74
     * @var EventDispatcher
75
     */
76
    protected $eventDispatcher;
77
78
    /**
79
     * @Inject(PaymentRepository::class)
80
     * @var PaymentRepository
81
     */
82
    protected $paymentRepository;
83
84
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
85
     * @Route("/{_admin}/setting/shop/payment", name="admin_setting_shop_payment")
86
     * @Template("Setting/Shop/payment.twig")
87
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
88 1 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
89
    {
90 1
        $Payments = $this->paymentRepository
91 1
            ->findBy(
92 1
                array(),
93 1
                array('rank' => 'DESC')
94
            );
95
96 1
        $event = new EventArgs(
97
            array(
98 1
                'Payments' => $Payments,
99
            ),
100 1
            $request
101
        );
102 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_INDEX_COMPLETE, $event);
103
104
        return [
105 1
            'Payments' => $Payments,
106
        ];
107
    }
108
109
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$Payment" missing
Loading history...
110
     * @Route("/{_admin}/setting/shop/payment/new", name="admin_setting_shop_payment_new")
111
     * @Route("/{_admin}/setting/shop/payment/{id}/edit", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_edit")
112
     * @Template("Setting/Shop/payment_edit.twig")
113
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
114 6
    public function edit(Application $app, Request $request, Payment $Payment = null)
115
    {
116 6
        if (is_null($Payment)) {
117
            // FIXME
118 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...
119 3
                ->findOrCreate(0);
120
        }
121
122 6
        $builder = $this->formFactory
123 6
            ->createBuilder(PaymentRegisterType::class, $Payment);
124
125 6
        $event = new EventArgs(
126
            array(
127 6
                'builder' => $builder,
128 6
                'Payment' => $Payment,
129
            ),
130 6
            $request
131
        );
132 6
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_INITIALIZE, $event);
133
134 6
        $form = $builder->getForm();
135 6
        $form->handleRequest($request);
136
137
        // 登録ボタン押下
138 6
        if ($form->isSubmitted() && $form->isValid()) {
139 2
            $Payment = $form->getData();
140
141
            // 手数料を設定できない場合には、手数料を0にする
142 2
            if ($Payment->getChargeFlg() == 2) {
143
                $Payment->setCharge(0);
144
            }
145
146
            // ファイルアップロード
147 2
            $file = $form['payment_image']->getData();
148 2
            $fs = new Filesystem();
149 2
            if ($file && $fs->exists($this->appConfig['image_temp_realdir'].'/'.$file)) {
150
                $fs->rename(
151
                    $this->appConfig['image_temp_realdir'].'/'.$file,
152
                    $this->appConfig['image_save_realdir'].'/'.$file
153
                );
154
            }
155
156 2
            $Payment->setVisible(true);
157 2
            $this->entityManager->persist($Payment);
158 2
            $this->entityManager->flush();
159
160 2
            $event = new EventArgs(
161
                array(
162 2
                    'form' => $form,
163 2
                    'Payment' => $Payment,
164
                ),
165 2
                $request
166
            );
167 2
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_EDIT_COMPLETE, $event);
168
169 2
            $app->addSuccess('admin.register.complete', 'admin');
170
171 2
            return $app->redirect($app->url('admin_setting_shop_payment'));
172
        }
173
174
        return [
175 4
            'form' => $form->createView(),
176 4
            'payment_id' => $Payment->getId(),
177 4
            'Payment' => $Payment,
178
        ];
179
    }
180
181
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
182
     * @Route("/{_admin}/setting/shop/payment/image/add", name="admin_payment_image_add")
183
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
184 2
    public function imageAdd(Application $app, Request $request)
185
    {
186 2
        if (!$request->isXmlHttpRequest()) {
187 1
            throw new BadRequestHttpException();
188
        }
189
190 1
        $images = $request->files->get('payment_register');
191 1
        $filename = null;
192 1
        if (isset($images['payment_image_file'])) {
193
            $image = $images['payment_image_file'];
194
195
            //ファイルフォーマット検証
196
            $mimeType = $image->getMimeType();
197
            if (0 !== strpos($mimeType, 'image')) {
198
                throw new UnsupportedMediaTypeHttpException();
199
            }
200
201
            $extension = $image->guessExtension();
202
            $filename = date('mdHis').uniqid('_').'.'.$extension;
203
            $image->move($this->appConfig['image_temp_realdir'], $filename);
204
        }
205 1
        $event = new EventArgs(
206
            array(
207 1
                'images' => $images,
208 1
                'filename' => $filename,
209
            ),
210 1
            $request
211
        );
212 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_IMAGE_ADD_COMPLETE, $event);
213 1
        $filename = $event->getArgument('filename');
214
215 1
        return $app->json(array('filename' => $filename), 200);
216
    }
217
218
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$TargetPayment" missing
Loading history...
219
     * @Method("DELETE")
220
     * @Route("/{_admin}/setting/shop/payment/{id}/delete", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_delete")
221
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
222 1
    public function delete(Application $app, Request $request, Payment $TargetPayment)
223
    {
224 1
        $this->isTokenValid($app);
225
226 1
        $rank = 1;
227 1
        $Payments = $this->paymentRepository->findBy(array(), array('rank' => 'ASC'));
228 1
        foreach ($Payments as $Payment) {
229 1
                $Payment->setRank($rank++);
230
        }
231
232
        try {
233 1
            $this->paymentRepository->delete($TargetPayment);
234 1
            $this->entityManager->flush();
235
236 1
            $event = new EventArgs(
237
                array(
238 1
                    'Payment' => $TargetPayment,
239
                ),
240 1
                $request
241
            );
242 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SHOP_PAYMENT_DELETE_COMPLETE, $event);
243
244 1
            $app->addSuccess('admin.delete.complete', 'admin');
245
        } catch(ForeignKeyConstraintViolationException $e) {
246
            $this->entityManager->rollback();
247
            $app->addError('admin.payment.delete.error', 'admin');
248
        }
249
250 1
        return $app->redirect($app->url('admin_setting_shop_payment'));
251
    }
252
253
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$current" missing
Loading history...
254
     * @Method("PUT")
255
     * @Route("/{_admin}/setting/shop/payment/{id}/up", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_up")
256
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
257 1 View Code Duplication
    public function up(Application $app, 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...
258
    {
259 1
        $this->isTokenValid($app);
260
261 1
        $currentRank = $current->getRank();
262 1
        $targetRank = $currentRank + 1;
263
264 1
        $target = $this->paymentRepository->findOneBy(array('rank' => $targetRank));
265
266 1
        $target->setRank($currentRank);
267 1
        $current->setRank($targetRank);
268
269 1
        $this->entityManager->flush();
270
271 1
        $app->addSuccess('admin.rank.move.complete', 'admin');
272
273 1
        return $app->redirect($app->url('admin_setting_shop_payment'));
274
    }
275
276
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$current" missing
Loading history...
277
     * @Method("PUT")
278
     * @Route("/{_admin}/setting/shop/payment/{id}/down", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_down")
279
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
280 1 View Code Duplication
    public function down(Application $app, 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...
281
    {
282 1
        $this->isTokenValid($app);
283
284 1
        $currentRank = $current->getRank();
285 1
        $targetRank = $currentRank - 1;
286
287 1
        $target = $this->paymentRepository->findOneBy(array('rank' => $targetRank));
288
289 1
        $target->setRank($currentRank);
290 1
        $current->setRank($targetRank);
291
292 1
        $this->entityManager->flush();
293
294 1
        $app->addSuccess('admin.rank.move.complete', 'admin');
295
296 1
        return $app->redirect($app->url('admin_setting_shop_payment'));
297
    }
298
299
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$Payment" missing
Loading history...
300
     * @Method("PUT")
301
     * @Route("/{_admin}/setting/shop/payment/{id}/visible", requirements={"id" = "\d+"}, name="admin_setting_shop_payment_visible")
302
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
303
    public function visible(Application $app, Payment $Payment)
304
    {
305
        $this->isTokenValid($app);
306
307
        $Payment->setVisible(!$Payment->isVisible());
308
309
        $this->entityManager->flush();
310
311
        if ($Payment->isVisible()) {
312
            $app->addSuccess('admin.payment.visible.complete', 'admin');
313
        } else {
314
            $app->addSuccess('admin.payment.invisible.complete', 'admin');
315
        }
316
317
        return $app->redirect($app->url('admin_setting_shop_payment'));
318
    }
319
}
320