CurrencyController::createAction()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 1
dl 0
loc 29
rs 8.5806
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\Currency;
15
use Newscoop\PaywallBundle\Form\Type\CurrencyType;
16
use Sylius\Component\Currency\Model\CurrencyInterface;
17
use Newscoop\PaywallBundle\Permissions;
18
19
class CurrencyController extends BaseController
20
{
21
    /**
22
     * @Route("/admin/paywall_plugin/currencies/", name="paywall_plugin_currency_index", options={"expose"=true})
23
     *
24
     * @Method("GET")
25
     */
26 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...
27
    {
28
        $this->hasPermission(Permissions::CURRENCIES_VIEW);
29
        $query = $this->getRepository()->findAllAvailable();
30
        $paginator = $this->get('knp_paginator');
31
        $currencies = $paginator->paginate(
32
            $query,
33
            $request->query->getInt('knp_page', 1),
34
            20
35
        );
36
37
        $currencies->setTemplate('NewscoopNewscoopBundle:Pagination:pagination_bootstrap3.html.twig');
38
39
        return $this->render('NewscoopPaywallBundle:Currency:index.html.twig', array(
40
            'currencies' => $currencies,
41
        ));
42
    }
43
44
    /**
45
     * @Route("/admin/paywall_plugin/currencies/create/", name="paywall_plugin_currency_create", options={"expose"=true})
46
     */
47
    public function createAction(Request $request)
48
    {
49
        $this->hasPermission(Permissions::CURRENCIES_MANAGE);
50
        $currency = $this->getRepository()->createNew();
51
        $form = $this->createForm(new CurrencyType(), $currency);
52
        $em = $this->get('em');
53
        $translator = $this->get('translator');
54
        if ($request->isMethod('POST')) {
55
            $form->handleRequest($request);
56
            if ($form->isValid()) {
57
                if (!$this->findByCode($currency)) {
58
                    $em->persist($currency);
59
                    $em->flush();
60
61
                    $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.created'));
62
                } else {
63
                    $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.success.exists'));
64
65
                    return $this->redirect($this->generateUrl('paywall_plugin_currency_create'));
66
                }
67
68
                return $this->redirect($this->generateUrl('paywall_plugin_currency_index'));
69
            }
70
        }
71
72
        return $this->render('NewscoopPaywallBundle:Currency:create.html.twig', array(
73
            'form' => $form->createView(),
74
        ));
75
    }
76
77
    /**
78
     * @Route("/admin/paywall_plugin/currencies/edit/{id}", name="paywall_plugin_currency_edit", options={"expose"=true})
79
     */
80 View Code Duplication
    public function editAction(Request $request, Currency $currency)
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...
81
    {
82
        $this->hasPermission(Permissions::CURRENCIES_MANAGE);
83
        $form = $this->createForm(new CurrencyType(), $currency);
84
        $em = $this->get('em');
85
        $translator = $this->get('translator');
86
        if ($request->isMethod('POST')) {
87
            $form->handleRequest($request);
88
            if ($form->isValid()) {
89
                if (!$this->checkForExistenceBy($currency)) {
90
                    $currency->setUpdatedAt(new \DateTime('now'));
91
                    $em->flush();
92
93
                    $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.saved'));
94
95
                    return $this->redirect($this->generateUrl('paywall_plugin_currency_index'));
96
                }
97
98
                $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.error.exists', array(
99
                    '%resource%' => $currency->getName(),
100
                )));
101
102
                return $this->redirect($this->generateUrl('paywall_plugin_currency_edit', array(
103
                    'id' => $currency->getId(),
104
                )));
105
            }
106
        }
107
108
        return $this->render('NewscoopPaywallBundle:Currency:edit.html.twig', array(
109
            'form' => $form->createView(),
110
            'currency' => $currency,
111
        ));
112
    }
113
114
    /**
115
     * @Route("/admin/paywall_plugin/currencies/delete/{id}", name="paywall_plugin_currency_delete", options={"expose"=true})
116
     *
117
     * @Method("DELETE")
118
     */
119 View Code Duplication
    public function deleteAction(Request $request, Currency $currency)
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...
120
    {
121
        $this->hasPermission(Permissions::CURRENCIES_MANAGE);
122
        $translator = $this->get('translator');
123
        if ($this->findByCode($currency)) {
124
            $em = $this->get('em');
125
            $em->remove($currency);
126
            $em->flush();
127
128
            $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.removed'));
129
        } else {
130
            $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.success.notexists'));
131
        }
132
133
        return $this->redirect($this->generateUrl('paywall_plugin_currency_index'));
134
    }
135
136
    private function findByCode(CurrencyInterface $currency)
137
    {
138
        if ($this->getRepository()->findOneByCode($currency->getCode())) {
139
            return true;
140
        }
141
142
        return false;
143
    }
144
145
    private function checkForExistenceBy(CurrencyInterface $currency)
146
    {
147
        $result = $this->getRepository()->checkIfExists($currency)
148
            ->getSingleScalarResult();
149
150
        if ((int) $result > 0) {
151
            return true;
152
        }
153
154
        return false;
155
    }
156
157
    private function getRepository()
158
    {
159
        $repository = $this->get('newscoop_paywall.currency.repository');
160
161
        return $repository;
162
    }
163
}
164