Completed
Pull Request — master (#168)
by
unknown
02:59
created

ProfileController::displayAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 7
Ratio 46.67 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 15
rs 9.4285
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Controller;
13
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
20
/**
21
 * Class SettingsListController. Is used for managing settings in General env.
22
 */
23
class ProfileController extends Controller
24
{
25
    /**
26
     * Renders add setting form.
27
     *
28
     * @return Response
29
     */
30
    public function displayAction()
31
    {
32
        $params = [];
33
        $cache = $this->get('es.cache_engine');
34 View Code Duplication
        if ($cache->contains('settings_errors')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
35
            $params['errors'] = $cache->fetch('settings_errors');
36
            $cache->delete('settings_errors');
37
        } elseif ($cache->contains('settings_success')) {
38
            $params['success'] = $cache->fetch('settings_success');
39
            $cache->delete('settings_success');
40
        }
41
        return $this->render(
42
            'ONGRSettingsBundle:Settings:addProfile.html.twig', $params
43
        );
44
    }
45
46
    /**
47
     * Action for creating a profile
48
     *
49
     * @param Request $request
50
     *
51
     * @return Response
52
     */
53
    public function createAction(Request $request)
54
    {
55
        $profileManager = $this->get('ongr_settings.profiles_manager');
56
        $profiles = $profileManager->getAllProfiles();
57
        $data = $this->get('ongr_settings.form_validator')->validateProfileForm($request, $profiles);
58
        $cache = $this->get('es.cache_engine');
59
60
        if ($data['error'] != '') {
61
            $cache->save('settings_errors', $data['error']);
62
            return new RedirectResponse($this->generateUrl('ongr_settings_profile_add'));
63
        }
64
        try {
65
            $profileManager->createProfile($data['name'], $data['description']);
66
        }catch (\Exception $e) {
67
            $cache->save('settings_errors', $e->getMessage());
68
            return new RedirectResponse($this->generateUrl('ongr_settings_profile_add'));
69
        }
70
        $cache->save('settings_success', true);
71
        return new RedirectResponse($this->generateUrl('ongr_settings_profile_add'));
72
    }
73
}
74