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