ConfigurePaywallController::indexAction()   C
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 76
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 49
nc 6
nop 1
dl 0
loc 76
rs 6.1941
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @author Rafał Muszyński <[email protected]>
5
 * @copyright 2013 Sourcefabric o.p.s.
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\Template;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Newscoop\PaywallBundle\Form\Type\SettingsFormType;
16
use Newscoop\PaywallBundle\Permissions;
17
18
class ConfigurePaywallController extends BaseController
19
{
20
    /**
21
     * @Route("/admin/paywall_plugin/configure-paywall", options={"expose"=true})
22
     * @Template()
23
     */
24
    public function indexAction(Request $request)
25
    {
26
        $this->hasPermission(Permissions::CONFIGURE);
27
        $em = $this->getDoctrine()->getManager();
28
        $preferencesService = $this->container->get('system_preferences_service');
29
        $translator = $this->container->get('translator');
30
        $active = $em->getRepository('Newscoop\PaywallBundle\Entity\Gateway')
31
            ->findOneBy(array(
32
                'isActive' => true,
33
            ));
34
35
        $form = $this->container->get('form.factory')->create(new SettingsFormType(), array(
36
            'notificationEmail' => $preferencesService->PaywallMembershipNotifyEmail,
37
            'enableNotify' => $preferencesService->PaywallEmailNotifyEnabled == '1' ? true : false,
38
            'notificationFromEmail' => $preferencesService->PaywallMembershipNotifyFromEmail,
39
            'currency' => $preferencesService->PaywallDefaultCurrency,
40
        ));
41
42
        if ($request->isMethod('POST')) {
43
            $form->bind($request);
44
            if ($form->isValid()) {
45
                $data = $form->getData();
46
                $preferencesService->set('PaywallMembershipNotifyEmail', $data['notificationEmail']);
47
                $preferencesService->set('PaywallEmailNotifyEnabled', $data['enableNotify']);
48
                $preferencesService->set('PaywallMembershipNotifyFromEmail', $data['notificationFromEmail']);
49
                $preferencesService->set('PaywallDefaultCurrency', $data['currency']);
50
51
                if (is_numeric($data['adapter'])) {
52
                    $settings = $em->getRepository('Newscoop\PaywallBundle\Entity\Gateway')
53
                        ->findOneBy(array(
54
                            'id' => $data['adapter'],
55
                        ));
56
57
                    $all = $em->getRepository('Newscoop\PaywallBundle\Entity\Gateway')
58
                        ->findAll();
59
60
                    foreach ($all as $value) {
61
                        $value->setActive(false);
62
                    }
63
64
                    $settings->setActive(true);
65
                    $em->flush();
66
                }
67
68
                $this->get('session')->getFlashBag()->add('success', $translator->trans('paywall.success.settingssaved'));
69
70
                return $this->redirect($this->generateUrl('newscoop_paywall_configurepaywall_index'));
71
            } else {
72
                $this->get('session')->getFlashBag()->add('error', $translator->trans('paywall.error.settingserror'));
73
74
                return $this->redirect($this->generateUrl('newscoop_paywall_configurepaywall_index'));
75
            }
76
        }
77
78
        if ($request->isXmlHttpRequest()) {
79
            $inactive = $em->getRepository('Newscoop\PaywallBundle\Entity\Gateway')
80
                ->findBy(array(
81
                    'isActive' => false,
82
                ));
83
84
            $adapters = array();
85
            foreach ($inactive as $value) {
86
                $adapters[] = array(
87
                    'id' => $value->getId(),
88
                    'text' => $value->getName(),
89
                );
90
            }
91
92
            return new Response(json_encode($adapters));
93
        }
94
95
        return array(
96
            'form' => $form->createView(),
97
            'current' => $active->getName(),
98
        );
99
    }
100
}
101