Completed
Push — master ( fdbeef...265a2d )
by jerome
25:01 queued 21:35
created

ConfigController::createConfigForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
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 DP\Core\CoreBundle\Controller;
13
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Yaml\Yaml;
17
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
18
use DP\Core\UserBundle\Entity\User;
19
20
class ConfigController extends Controller
21
{
22
    private $configFile;
23
24
    public function __construct()
25
    {
26
        $this->configFile = __DIR__ . '/../../../../../app/config/dedipanel.yml';
27
    }
28
29
    public function configAction(Request $request)
30
    {
31
        if (!$this->get('security.context')->isGranted(User::ROLE_SUPER_ADMIN)) {
32
            throw new AccessDeniedException();
33
        }
34
35
        $debugMode = $this->container->getParameter('dedipanel.debug');
36
        $usable = $this->verifyConfigFile();
37
38
        $form = $this->createConfigForm(array(
39
            'debug_mode' => $debugMode,
40
        ), !$usable);
41
42
        if ($request->isMethod('POST') && $form->submit($request)->isValid()
43
        && $usable) {
44
            $data = $form->getData();
45
            $debugMode = (bool) $data['debug_mode'];
46
47
            if ($this->updateConfigFile($debugMode)) {
48
                $this->addFlash('success', 'dedipanel.core.config.update_succeeded');
49
            }
50
            else {
51
                $this->addFlash('error', 'dedipanel.core.config.update_failed');
52
            }
53
54
            return $this->redirect($this->generateUrl('dedipanel_core_config'));
55
        }
56
57
        if ($this->container->getParameter('kernel.environment') == 'prod') {
58
            $this->verifyUpdate();
59
        }
60
61
        return $this->render('DPCoreBundle:Config:index.html.twig', array(
62
            'form' => $form->createView(),
63
        ));
64
    }
65
66
    private function createConfigForm(array $default = array(), $disabled = false)
67
    {
68
        $form = $this
69
            ->createFormBuilder($default)
70
            ->add('debug_mode', 'choice', array(
71
                'choices' => array('Non', 'Oui'),
72
                'disabled' => $disabled,
73
            ))
74
        ;
75
76
        return $form->getForm();
77
    }
78
79
    /**
80
     * Verify if the config file (dedipanel.yml) is accessible and writable.
81
     *
82
     * @return bool
83
     */
84
    private function verifyConfigFile()
85
    {
86
        if (!file_exists($this->configFile)) {
87
            $this->addFlash('error', 'dedipanel.core.config.file_not_found');
88
89
            return false;
90
        }
91
92
        if (!is_writable($this->configFile)) {
93
            $this->addFlash('error', 'dedipanel.core.config.file_not_writable');
94
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * @param boolean $debugMode
103
     */
104
    private function updateConfigFile($debugMode)
105
    {
106
        $config = array(
107
            'dp_core' => array(
108
                'version' => $this->container->getParameter('dedipanel.version'),
109
                'debug' => $debugMode,
110
            ),
111
        );
112
113
        $yaml = Yaml::dump($config, 2);
114
115
        return (bool) file_put_contents($this->configFile, $yaml);
116
    }
117
118
    /**
119
     * @param string $type
120
     * @param string $message
121
     */
122
    private function addFlash($type, $message, $params = array())
123
    {
124
        $message = $this->get('translator')->trans($message, $params, 'flashes');
125
126
        /** @var FlashBag $flashBag */
127
        $flashBag = $this->get('session')->getBag('flashes');
128
        $flashBag->add($type, $message);
129
    }
130
131
    private function verifyUpdate()
132
    {
133
        /** @var DP\Core\CoreBundle\Service\UpdateWatcherService $watcher */
134
        $watcher = $this->get('dp_core.update_watcher.service');
135
136
        if ($watcher->isUpdateAvailable()) {
137
            $this->addFlash('warning', 'dedipanel.core.update_available', array(
138
                '%version%' => 'v' . $watcher->getAvailableVersion(),
139
                '%url%' => 'http://www.dedicated-panel.net',
140
            ));
141
        }
142
    }
143
}
144