Completed
Push — master ( 265a2d...f9e310 )
by jerome
02:58
created

ConfigController::verifyConfigFile()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 9
nc 6
nop 0
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 DP\Core\CoreBundle\Settings\Settings;
15
use DP\Core\CoreBundle\Settings\SettingsReader;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\Form\Form;
18
use Symfony\Component\Form\FormBuilder;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Yaml\Yaml;
21
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
22
use DP\Core\UserBundle\Entity\User;
23
24
class ConfigController extends Controller
25
{
26
    public function configAction(Request $request)
27
    {
28
        if (!$this->get('security.context')->isGranted(User::ROLE_SUPER_ADMIN)) {
29
            throw new AccessDeniedException();
30
        }
31
32
        /** @var Settings $settings */
33
        $settings = $this->get('dedipanel.core_settings.settings');
34
        $usable   = $this->verifyConfigFile();
35
36
        $form = $this->createForm('core_settings', $settings, ['disabled' => !$usable]);
37
38
        if ($form->handleRequest($request) && $form->isValid() && $usable) {
39
            $settings = $form->getData();
40
41
            $flashMsg  = 'update_succeeded';
42
            $flashType = 'success';
43
44
            if (!$this->get('dedipanel.core_settings.writer')->write($settings)) {
45
                $flashMsg  = 'update_failed';
46
                $flashType = 'error';
47
            }
48
49
            $this->addFlash($flashType, sprintf('dedipanel.core.config.%s', $flashMsg));
50
51
            return $this->redirectToRoute('dedipanel_core_config');
52
        }
53
54
        $this->verifyUpdate();
55
56
        return $this->render('DPCoreBundle:Config:index.html.twig', array(
57
            'form' => $form->createView(),
58
        ));
59
    }
60
61
    /**
62
     * Verify if the config file (dedipanel.yml) is accessible and writable.
63
     *
64
     * @return bool
65
     */
66
    private function verifyConfigFile()
67
    {
68
        /** @var SettingsReader $reader */
69
        $error = '';
70
71
        if (!$this->get('dedipanel.core_settings.reader')->fileExists()) {
72
            $error = 'file_not_found';
73
        } elseif (!$this->get('dedipanel.core_settings.writer')->isWritable()) {
74
            $error = 'file_not_writable';
75
        }
76
77
        if (!empty($error)) {
78
            $this->addFlash('error', sprintf('dedipanel.core.config.%s', $error));
79
        }
80
81
        return empty($error);
82
    }
83
84
    /**
85
     * @param string $type
86
     * @param string $message
87
     * @param array $params
88
     */
89
    protected function addFlash($type, $message, array $params = array())
90
    {
91
        $message = $this->get('translator')->trans($message, $params, 'flashes');
92
93
        /** @var FlashBag $flashBag */
94
        $flashBag = $this->get('session')->getBag('flashes');
95
        $flashBag->add($type, $message);
96
    }
97
98
    /**
99
     * Will check if a new update is available, and will display a flash message if it is the case
100
     */
101
    private function verifyUpdate()
102
    {
103
        if ('prod' != $this->container->getParameter('kernel.environment')) {
104
            return;
105
        }
106
107
        /** @var \DP\Core\CoreBundle\Service\UpdateWatcherService $watcher */
108
        $watcher = $this->get('dp_core.update_watcher.service');
109
110
        if ($watcher->isUpdateAvailable()) {
111
            $this->addFlash('warning', 'dedipanel.core.update_available', array(
112
                '%version%' => 'v' . $watcher->getAvailableVersion(),
113
                '%url%' => 'http://www.dedicated-panel.net',
114
            ));
115
        }
116
    }
117
}
118