Completed
Pull Request — master (#24)
by Rafał
03:01
created

PaymentController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 0
cbo 5
dl 0
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 16 1
A editAction() 0 22 3
A deleteAction() 0 15 2
A getRepository() 0 6 1
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
namespace Newscoop\PaywallBundle\Controller;
9
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Symfony\Component\HttpFoundation\Request;
13
use Newscoop\PaywallBundle\Entity\Payment;
14
use Newscoop\PaywallBundle\Form\Type\PaymentType;
15
16
class PaymentController extends BaseController
17
{
18
    /**
19
     * @Route("/admin/paywall_plugin/payments/", name="paywall_plugin_payment_index", options={"expose"=true})
20
     *
21
     * @Method("GET")
22
     */
23
    public function indexAction(Request $request)
24
    {
25
        $query = $this->getRepository()->findAllAvailable();
26
        $paginator = $this->get('knp_paginator');
27
        $payments = $paginator->paginate(
28
            $query,
29
            $request->query->getInt('knp_page', 1),
30
            20
31
        );
32
33
        $payments->setTemplate('NewscoopNewscoopBundle:Pagination:pagination_bootstrap3.html.twig');
34
35
        return $this->render('NewscoopPaywallBundle:Payment:index.html.twig', array(
36
            'payments' => $payments,
37
        ));
38
    }
39
40
    /**
41
     * @Route("/admin/paywall_plugin/payments/edit/{id}", name="paywall_plugin_payment_edit", options={"expose"=true})
42
     */
43
    public function editAction(Request $request, Payment $payment)
44
    {
45
        $form = $this->createForm(new PaymentType(), $payment);
46
        $entityManager = $this->get('em');
47
        $translator = $this->get('translator');
48
        if ($request->isMethod('POST')) {
49
            $form->handleRequest($request);
50
            if ($form->isValid()) {
51
                $payment->setUpdatedAt(new \DateTime('now'));
52
                $entityManager->flush();
53
54
                $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.saved'));
55
56
                return $this->redirect($this->generateUrl('paywall_plugin_payment_index'));
57
            }
58
        }
59
60
        return $this->render('NewscoopPaywallBundle:Payment:edit.html.twig', array(
61
            'form' => $form->createView(),
62
            'payment' => $payment,
63
        ));
64
    }
65
66
    /**
67
     * @Route("/admin/paywall_plugin/payments/delete/{id}", name="paywall_plugin_payment_delete", options={"expose"=true})
68
     *
69
     * @Method("DELETE")
70
     */
71
    public function deleteAction(Payment $payment)
72
    {
73
        $translator = $this->get('translator');
74
        if ($this->getRepository()->findOneById($payment->getId())) {
75
            $entityManager = $this->get('em');
76
            $entityManager->remove($payment);
77
            $entityManager->flush();
78
79
            $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.removed'));
80
        } else {
81
            $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.success.notexists'));
82
        }
83
84
        return $this->redirect($this->generateUrl('paywall_plugin_payment_index'));
85
    }
86
87
    private function getRepository()
88
    {
89
        $repository = $this->get('newscoop_paywall.services.payment')->getRepository();
90
91
        return $repository;
92
    }
93
}
94