Completed
Push — master ( 536bbe...ea77da )
by Julito
10:11
created

SettingsController::updateAction()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 54
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 36
nc 9
nop 3
dl 0
loc 54
rs 9.0306
c 1
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
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CourseBundle\Controller;
5
6
use Chamilo\CourseBundle\Manager\SettingsManager;
7
use Sylius\Bundle\SettingsBundle\Form\Factory\SettingsFormFactoryInterface;
8
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Translation\TranslatorInterface;
13
use Symfony\Component\Validator\Exception\ValidatorException;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
15
16
/**
17
 * Settings controller.
18
 *
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 */
21
class SettingsController extends Controller
22
{
23
    /**
24
     * Edit configuration with given namespace.
25
     *
26
     * @param Request $request
27
     * @param string $namespace
28
     * @ParamConverter("course", class="ChamiloCoreBundle:Course", options={"repository_method" = "findOneByCode"})
29
     *
30
     * @return Response
31
     */
32
    public function updateAction(Request $request, $namespace, $course)
33
    {
34
        $manager = $this->getSettingsManager();
35
36
        $schemaAlias = $manager->convertNameSpaceToService($namespace);
37
        $settings = $manager->load($namespace);
38
39
        $form = $this
40
            ->getSettingsFormFactory()
41
            ->create($schemaAlias);
42
43
        $form->setData($settings);
44
45
        if ($form->handleRequest($request)->isValid()) {
46
            $messageType = 'success';
47
            try {
48
                $manager->setCourse($course);
49
                $manager->saveSettings($namespace, $form->getData());
50
                $message = $this->getTranslator()->trans(
51
                    'sylius.settings.update',
52
                    [],
53
                    'flashes'
54
                );
55
            } catch (ValidatorException $exception) {
56
                $message = $this->getTranslator()->trans(
57
                    $exception->getMessage(),
58
                    [],
59
                    'validators'
60
                );
61
                $messageType = 'error';
62
            }
63
            $request->getSession()->getBag('flashes')->add(
64
                $messageType,
65
                $message
66
            );
67
68
            if ($request->headers->has('referer')) {
69
                return $this->redirect($request->headers->get('referer'));
70
            }
71
        }
72
        $schemas = $manager->getSchemas();
73
74
        return $this->render(
75
            $request->attributes->get(
76
                'template',
77
                'ChamiloCourseBundle:Settings:update.html.twig'
78
            ),
79
            [
80
                'course' => $course,
81
                'schemas' => $schemas,
82
                'settings' => $settings,
83
                'form' => $form->createView(),
84
                'keyword' => '',
85
                'search_form' => '',
86
            ]
87
        );
88
    }
89
90
    /**
91
     * Get settings manager.
92
     *
93
     * @return SettingsManager
94
     */
95
    protected function getSettingsManager()
96
    {
97
        return $this->get('chamilo_course.settings.manager');
98
    }
99
100
    /**
101
     * Get settings form factory.
102
     *
103
     * @return SettingsFormFactoryInterface
104
     */
105
    protected function getSettingsFormFactory()
106
    {
107
        return $this->get('chamilo_course.settings.form_factory');
108
    }
109
110
    /**
111
     * Get translator.
112
     *
113
     * @return TranslatorInterface
114
     */
115
    protected function getTranslator()
116
    {
117
        return $this->get('translator');
118
    }
119
}
120