GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SettingsController::indexAction()   C
last analyzed

Complexity

Conditions 8
Paths 24

Size

Total Lines 79
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 6.0572
c 0
b 0
f 0
cc 8
eloc 44
nc 24
nop 2

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
namespace Application\Controller\MembersArea;
4
5
use Application\Form\Type\SettingsType;
6
use Application\Entity\SettingEntity;
7
use Silex\Application;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @author Borut Balažek <[email protected]>
13
 */
14
class SettingsController
15
{
16
    /**
17
     * @param Request     $request
18
     * @param Application $app
19
     *
20
     * @return Response
21
     */
22
    public function indexAction(Request $request, Application $app)
23
    {
24
        if (!$app['security']->isGranted('ROLE_ADMIN')) {
25
            $app->abort(403);
26
        }
27
28
        $form = $app['form.factory']->create(
29
            new SettingsType($app)
30
        );
31
32
        if ($request->getMethod() == 'POST') {
33
            $form->handleRequest($request);
34
35
            if ($form->isValid()) {
36
                $data = $form->getData();
37
38
                if (!empty($data)) {
39
                    foreach ($data as $key => $value) {
40
                        $settingEntity = $app['orm.em']
41
                            ->getRepository('Application\Entity\SettingEntity')
42
                            ->findOneByKey($key)
43
                        ;
44
45
                        if ($settingEntity === null) {
46
                            $settingEntity = new SettingEntity();
47
48
                            $settingEntity
49
                                ->setKey($key)
50
                            ;
51
                        }
52
53
                        $settingEntity
54
                            ->setValue($value)
55
                        ;
56
57
                        $app['orm.em']->persist($settingEntity);
58
                    }
59
60
                    try {
61
                        $app['orm.em']->flush();
62
63
                        $app['flashbag']->add(
64
                            'success',
65
                            $app['translator']->trans(
66
                                'The settings were successfully saved!'
67
                            )
68
                        );
69
                    } catch (\Exception $e) {
70
                        $app['flashbag']->add(
71
                            'danger',
72
                            $e->getMessage()
73
                        );
74
                    }
75
76
                    return $app->redirect(
77
                        $app['url_generator']->generate(
78
                            'members-area.settings'
79
                        )
80
                    );
81
                } else {
82
                    $app['flashbag']->add(
83
                        'info',
84
                        $app['translator']->trans(
85
                            'No changes were saved!'
86
                        )
87
                    );
88
                }
89
            }
90
        }
91
92
        return new Response(
93
            $app['twig']->render(
94
                'contents/members-area/settings/index.html.twig',
95
                array(
96
                    'form' => $form->createView(),
97
                )
98
            )
99
        );
100
    }
101
}
102