Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

ConfigBundle/Controller/ConfigController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\ConfigBundle\Controller;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\ConfigBundle\Entity\AbstractConfig;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\Form\FormFactoryInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Symfony\Component\Routing\RouterInterface;
15
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
18
/**
19
 * Class ConfigController
20
 * @package Kunstmaan\ConfigBundle\Controller
21
 *
22
 * @Route(service="kunstmaan_config.controller.config")
23
 */
24
class ConfigController
25
{
26
    /**
27
     * @var RouterInterface
28
     */
29
    private $router;
30
31
    /**
32
     * @var EngineInterface
33
     */
34
    private $templating;
35
36
    /**
37
     * @var AuthorizationCheckerInterface
38
     */
39
    private $authorizationChecker;
40
41
    /**
42
     * @var EntityManagerInterface $em
43
     */
44
    private $em;
45
46
    /**
47
     * @var array $configuration
48
     */
49
    private $configuration;
50
51
    /**
52
     * @var FormFactoryInterface $formFactory
53
     */
54
    private $formFactory;
55
56
    /**
57
     * @param RouterInterface $router
58
     * @param EngineInterface $templating
59
     * @param AuthorizationCheckerInterface $authorizationChecker
60
     * @param EntityManagerInterface $em
61
     * @param array $configuration
62
     * @param ContainerInterface $container
63
     * @param FormFactoryInterface $formFactory
64
     */
65
    public function __construct(
66
        RouterInterface $router,
67
        EngineInterface $templating,
68
        AuthorizationCheckerInterface $authorizationChecker,
69
        EntityManagerInterface $em,
70
        array $configuration,
71
        /* ContainerInterface $container, */
72
        /* FormFactoryInterface */ $formFactory
73
    ) {
74
        $this->router = $router;
75
        $this->templating = $templating;
76
        $this->authorizationChecker = $authorizationChecker;
77
        $this->em = $em;
78
        $this->configuration = $configuration;
79
80 View Code Duplication
        if (func_num_args() > 6) {
0 ignored issues
show
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...
81
            @trigger_error(sprintf('Passing the "container" as the sixth argument in "%s" is deprecated in KunstmaanConfigBundle 5.1 and will be removed in KunstmaanConfigBundle 6.0. Remove the "container" argument from your service definition.', __METHOD__), E_USER_DEPRECATED);
82
83
            $this->formFactory = func_get_arg(6);
84
85
            return;
86
        }
87
88
        $this->formFactory = $formFactory;
89
    }
90
91
    /**
92
     * Generates the site config administration form and fills it with a default value if needed.
93
     *
94
     * @param Request $request
95
     * @param string $internalName
96
     *
97
     * @return array|RedirectResponse
98
     */
99
    public function indexAction(Request $request, $internalName)
100
    {
101
        /**
102
         * @var $entity AbstractConfig
103
         */
104
        $entity = $this->getConfigEntityByInternalName($internalName);
105
        $entityClass = get_class($entity);
106
107
        // Check if current user has permission for the site config.
108
        foreach ($entity->getRoles() as $role) {
109
            $this->checkPermission($role);
110
        }
111
112
        $repo = $this->em->getRepository($entityClass);
113
        $config = $repo->findOneBy(array());
114
115
        if (!$config) {
116
            $config = new $entityClass();
117
        }
118
119
        $form = $this->formFactory->create(
120
            $entity->getDefaultAdminType(),
121
            $config
122
        );
123
124
        if ($request->isMethod('POST')) {
125
            $form->handleRequest($request);
126
127
            if ($form->isSubmitted() && $form->isValid()) {
128
                $this->em->persist($config);
129
                $this->em->flush();
130
131
                return new RedirectResponse($this->router->generate('kunstmaanconfigbundle_default', array('internalName' => $internalName)));
132
            }
133
        }
134
135
        return $this->templating->renderResponse(
136
            '@KunstmaanConfig/Settings/configSettings.html.twig',
137
            array(
138
                'form' => $form->createView(),
139
            )
140
        );
141
    }
142
143
    /**
144
     * Get site config entity by a given internal name
145
     * If entity not found, throw new NotFoundHttpException()
146
     *
147
     * @param string $internalName
148
     *
149
     * @return AbstractConfig
150
     * @throws NotFoundHttpException
151
     */
152
    private function getConfigEntityByInternalName($internalName)
153
    {
154
        foreach ($this->configuration['entities'] as $class) {
155
            /** @var AbstractConfig $entity */
156
            $entity = new $class;
157
158
            if ($entity->getInternalName() == $internalName) {
159
                return $entity;
160
            }
161
        }
162
163
        throw new NotFoundHttpException();
164
    }
165
166
    /**
167
     * Check permission
168
     *
169
     * @param string $roleToCheck
170
     *
171
     * @throws AccessDeniedException
172
     */
173
    private function checkPermission($roleToCheck = 'ROLE_SUPER_ADMIN')
174
    {
175
        if (false === $this->authorizationChecker->isGranted($roleToCheck)) {
176
            throw new AccessDeniedException();
177
        }
178
    }
179
}
180