Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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 Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
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
use Twig\Environment;
18
19
/**
20
 * Class ConfigController
21
 */
22
class ConfigController
23
{
24
    /**
25
     * @var RouterInterface
26
     */
27
    private $router;
28
29
    /**
30
     * @var EngineInterface|Environment
31
     */
32
    private $twig;
33
34
    /**
35
     * @var AuthorizationCheckerInterface
36
     */
37
    private $authorizationChecker;
38
39
    /**
40
     * @var EntityManagerInterface
41
     */
42
    private $em;
43
44
    /**
45
     * @var array
46
     */
47
    private $configuration;
48
49
    /**
50
     * @var FormFactoryInterface
51
     */
52
    private $formFactory;
53
54
    /**
55
     * @param EngineInterface|Environment $twig
56
     * @param ContainerInterface          $container
0 ignored issues
show
There is no parameter named $container. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
     * @param FormFactoryInterface        $formFactory
58
     */
59
    public function __construct(
60
        RouterInterface $router,
61
        /* Environment */ $twig,
62
        AuthorizationCheckerInterface $authorizationChecker,
63
        EntityManagerInterface $em,
64
        array $configuration,
65
        /* ContainerInterface $container, */
66
        /* FormFactoryInterface */ $formFactory
67
    ) {
68
        $this->router = $router;
69
        $this->twig = $twig;
70
        $this->authorizationChecker = $authorizationChecker;
71
        $this->em = $em;
72
        $this->configuration = $configuration;
73
74
        if ($twig instanceof EngineInterface) {
75
            @trigger_error('Passing the "@templating" service as the 2nd argument is deprecated since KunstmaanConfigBundle 5.4 and will be replaced by the Twig renderer in KunstmaanConfigBundle 6.0. Injected the "@twig" service instead.', E_USER_DEPRECATED);
76
        }
77
78 View Code Duplication
        if (\func_num_args() > 6) {
79
            @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);
80
81
            $this->formFactory = func_get_arg(6);
82
83
            return;
84
        }
85
86
        $this->formFactory = $formFactory;
87
    }
88
89
    /**
90
     * Generates the site config administration form and fills it with a default value if needed.
91
     *
92
     * @param string $internalName
93
     *
94
     * @return Response
95
     */
96
    public function indexAction(Request $request, $internalName)
97
    {
98
        /**
99
         * @var AbstractConfig
100
         */
101
        $entity = $this->getConfigEntityByInternalName($internalName);
102
        $entityClass = \get_class($entity);
103
104
        // Check if current user has permission for the site config.
105
        foreach ($entity->getRoles() as $role) {
106
            $this->checkPermission($role);
107
        }
108
109
        $repo = $this->em->getRepository($entityClass);
110
        $config = $repo->findOneBy([]);
111
112
        if (!$config) {
113
            $config = new $entityClass();
114
        }
115
116
        $form = $this->formFactory->create(
117
            $entity->getDefaultAdminType(),
118
            $config
119
        );
120
121
        if ($request->isMethod('POST')) {
122
            $form->handleRequest($request);
123
124
            if ($form->isSubmitted() && $form->isValid()) {
125
                $this->em->persist($config);
126
                $this->em->flush();
127
128
                return new RedirectResponse($this->router->generate('kunstmaanconfigbundle_default', ['internalName' => $internalName]));
129
            }
130
        }
131
132
        return new Response(
133
            $this->twig->render('@KunstmaanConfig/Settings/configSettings.html.twig', ['form' => $form->createView()])
134
        );
135
    }
136
137
    /**
138
     * Get site config entity by a given internal name
139
     * If entity not found, throw new NotFoundHttpException()
140
     *
141
     * @param string $internalName
142
     *
143
     * @return AbstractConfig
144
     *
145
     * @throws NotFoundHttpException
146
     */
147
    private function getConfigEntityByInternalName($internalName)
148
    {
149
        foreach ($this->configuration['entities'] as $class) {
150
            /** @var AbstractConfig $entity */
151
            $entity = new $class();
152
153
            if ($entity->getInternalName() == $internalName) {
154
                return $entity;
155
            }
156
        }
157
158
        throw new NotFoundHttpException();
159
    }
160
161
    /**
162
     * Check permission
163
     *
164
     * @param string $roleToCheck
165
     *
166
     * @throws AccessDeniedException
167
     */
168
    private function checkPermission($roleToCheck = 'ROLE_SUPER_ADMIN')
169
    {
170
        if (false === $this->authorizationChecker->isGranted($roleToCheck)) {
171
            throw new AccessDeniedException();
172
        }
173
    }
174
}
175