DiscountController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 15
loc 15
rs 9.4285
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\Form\Type\DiscountType;
15
use Newscoop\PaywallBundle\Entity\Discount;
16
use Newscoop\PaywallBundle\Permissions;
17
18
class DiscountController extends BaseController
19
{
20
    /**
21
     * @Route("/admin/paywall_plugin/discounts", options={"expose"=true})
22
     */
23 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...
24
    {
25
        $this->hasPermission(Permissions::DISCOUNTS_VIEW);
26
        $query = $this->getDiscountRepository()->findActive();
27
        $paginator = $this->get('knp_paginator');
28
        $pagination = $paginator->paginate(
29
            $query,
30
            $request->query->getInt('page', 1),
31
            10
32
        );
33
34
        return $this->render('NewscoopPaywallBundle:Discount:index.html.twig', array(
35
            'pagination' => $pagination,
36
        ));
37
    }
38
39
    /**
40
     * @Route("/admin/paywall_plugin/discounts/create/", options={"expose"=true}, name="paywall_plugin_discount_create")
41
     */
42
    public function createAction(Request $request)
43
    {
44
        $this->hasPermission(Permissions::DISCOUNTS_MANAGE);
45
        $discount = new Discount();
46
        $form = $this->createForm(new DiscountType(), $discount);
47
        $em = $this->get('em');
48
        $translator = $this->get('translator');
49
        if ($request->isMethod('POST')) {
50
            $form->handleRequest($request);
51
            if ($form->isValid()) {
52
                if (!$this->exists($discount)) {
53
                    $em->persist($discount);
54
                    $em->flush();
55
56
                    $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.created'));
57
                } else {
58
                    $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.success.exists'));
59
                }
60
61
                return $this->redirect($this->generateUrl('newscoop_paywall_discount_index'));
62
            }
63
        }
64
65
        return $this->render('NewscoopPaywallBundle:Discount:create.html.twig', array(
66
            'form' => $form->createView(),
67
        ));
68
    }
69
70
    /**
71
     * @Route("/admin/paywall_plugin/discounts/delete/{id}", options={"expose"=true}, name="paywall_plugin_discount_delete")
72
     *
73
     * @Method("DELETE")
74
     */
75 View Code Duplication
    public function deleteAction(Request $request, Discount $discount)
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...
76
    {
77
        $this->hasPermission(Permissions::DISCOUNTS_MANAGE);
78
        $translator = $this->get('translator');
79
        if ($this->exists($discount)) {
80
            $em = $this->get('em');
81
            $em->remove($discount);
82
            $em->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('newscoop_paywall_discount_index'));
90
    }
91
92
    /**
93
     * @Route("/admin/paywall_plugin/discounts/edit/{id}", options={"expose"=true}, name="paywall_plugin_discount_edit")
94
     */
95 View Code Duplication
    public function editAction(Request $request, Discount $discount)
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...
96
    {
97
        $this->hasPermission(Permissions::DISCOUNTS_MANAGE);
98
        $form = $this->createForm(new DiscountType(), $discount);
99
        $em = $this->get('em');
100
        $translator = $this->get('translator');
101
        if ($request->isMethod('POST')) {
102
            $form->handleRequest($request);
103
            if ($form->isValid()) {
104
                if (!$this->checkForExistenceBy($discount)) {
105
                    $discount->setUpdatedAt(new \DateTime('now'));
106
                    $em->flush();
107
108
                    $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.saved'));
109
110
                    return $this->redirect($this->generateUrl('newscoop_paywall_discount_index'));
111
                }
112
113
                $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.success.exists'));
114
115
                return $this->redirect($this->generateUrl('paywall_plugin_discount_edit', array(
116
                    'id' => $discount->getId(),
117
                )));
118
            }
119
        }
120
121
        return $this->render('NewscoopPaywallBundle:Discount:edit.html.twig', array(
122
            'form' => $form->createView(),
123
            'discountId' => $discount->getId(),
124
        ));
125
    }
126
127
    private function exists(Discount $discount)
128
    {
129
        if ($this->getDiscountRepository()->findOneByName($discount->getName())) {
130
            return true;
131
        }
132
133
        return false;
134
    }
135
136
    private function checkForExistenceBy(Discount $discount)
137
    {
138
        $result = $this->getDiscountRepository()->createQueryBuilder('d')
139
            ->select('count(d)')
140
            ->where('d.name = :name')
141
            ->andWhere('d.id <> :id')
142
            ->setParameter('name', $discount->getName())
143
            ->setParameter('id', $discount->getId())
144
            ->getQuery()
145
            ->getSingleScalarResult();
146
147
        if ((int) $result > 0) {
148
            return true;
149
        }
150
151
        return false;
152
    }
153
154
    private function getDiscountRepository()
155
    {
156
        $em = $this->get('em');
157
158
        return $em->getRepository('Newscoop\PaywallBundle\Entity\Discount');
159
    }
160
}
161