PaymentController::editAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2015 Sourcefabric z.ú.
6
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
7
 */
8
9
namespace Newscoop\PaywallBundle\Controller;
10
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Symfony\Component\HttpFoundation\Request;
14
use Newscoop\PaywallBundle\Entity\Payment;
15
use Newscoop\PaywallBundle\Form\Type\PaymentType;
16
use Newscoop\PaywallBundle\Permissions;
17
18
class PaymentController extends BaseController
19
{
20
    /**
21
     * @Route("/admin/paywall_plugin/payments/", name="paywall_plugin_payment_index", options={"expose"=true})
22
     *
23
     * @Method("GET")
24
     */
25 View Code Duplication
    public function indexAction(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...
26
    {
27
        $this->hasPermission(Permissions::PAYMENTS_VIEW);
28
        $query = $this->getRepository()->findAllAvailable();
29
        $paginator = $this->get('knp_paginator');
30
        $payments = $paginator->paginate(
31
            $query,
32
            $request->query->getInt('knp_page', 1),
33
            20
34
        );
35
36
        $payments->setTemplate('NewscoopNewscoopBundle:Pagination:pagination_bootstrap3.html.twig');
37
38
        return $this->render('NewscoopPaywallBundle:Payment:index.html.twig', array(
39
            'payments' => $payments,
40
        ));
41
    }
42
43
    /**
44
     * @Route("/admin/paywall_plugin/payments/edit/{id}", name="paywall_plugin_payment_edit", options={"expose"=true})
45
     */
46
    public function editAction(Request $request, Payment $payment)
47
    {
48
        $this->hasPermission(Permissions::PAYMENTS_MANAGE);
49
        $form = $this->createForm(new PaymentType(), $payment);
50
        $entityManager = $this->get('em');
51
        $translator = $this->get('translator');
52
        if ($request->isMethod('POST')) {
53
            $form->handleRequest($request);
54
            if ($form->isValid()) {
55
                $payment->setUpdatedAt(new \DateTime('now'));
56
                $entityManager->flush();
57
58
                $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.saved'));
59
60
                return $this->redirect($this->generateUrl('paywall_plugin_payment_index'));
61
            }
62
        }
63
64
        return $this->render('NewscoopPaywallBundle:Payment:edit.html.twig', array(
65
            'form' => $form->createView(),
66
            'payment' => $payment,
67
        ));
68
    }
69
70
    /**
71
     * @Route("/admin/paywall_plugin/payments/delete/{id}", name="paywall_plugin_payment_delete", options={"expose"=true})
72
     *
73
     * @Method("DELETE")
74
     */
75
    public function deleteAction(Payment $payment)
76
    {
77
        $this->hasPermission(Permissions::PAYMENTS_MANAGE);
78
        $translator = $this->get('translator');
79
        if ($this->getRepository()->findOneById($payment->getId())) {
80
            $entityManager = $this->get('em');
81
            $entityManager->remove($payment);
82
            $entityManager->flush();
83
84
            $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.removed'));
85
        } else {
86
            $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.success.notexists'));
87
        }
88
89
        return $this->redirect($this->generateUrl('paywall_plugin_payment_index'));
90
    }
91
92
    private function getRepository()
93
    {
94
        $repository = $this->get('newscoop_paywall.services.payment')->getRepository();
95
96
        return $repository;
97
    }
98
}
99