Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

CacheBundle/Controller/VarnishController.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\CacheBundle\Controller;
4
5
use Kunstmaan\CacheBundle\Form\Varnish\BanType;
6
use Kunstmaan\NodeBundle\Entity\Node;
7
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
/**
16
 * Class VarnishController.
17
 */
18
class VarnishController extends Controller
19
{
20
    /**
21
     * Generates the varnish ban form.
22
     *
23
     * @Route("/settings/varnish", name="kunstmaancachebundle_varnish_settings_ban")
24
     * @Template("KunstmaanCacheBundle:Varnish:ban.html.twig")
25
     *
26
     * @param Request $request
27
     *
28
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,\Symfony\Component\Form\FormView>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
29
     */
30
    public function indexAction(Request $request)
31
    {
32
        $this->checkPermission();
33
34
        $form = $this->createForm(BanType::class);
35
36
        if ($request->isMethod('POST')) {
37
            $form->handleRequest($request);
38
39
            if ($form->isValid()) {
40
                $path = $form['path']->getData();
41
42
                $this->get('kunstmaan_cache.helper.varnish')->banPath($path, $form['allDomains']->getData());
43
44
                $this->addFlash('success', 'kunstmaan_cache.varnish.ban.success');
45
            }
46
        }
47
48
        return [
49
            'form' => $form->createView(),
50
        ];
51
    }
52
53
    /**
54
     * Ban route from varnish
55
     *
56
     * @Route("/varnish/ban/{node}", name="kunstmaancachebundle_varnish_ban")
57
     *
58
     * @param Node $node
59
     *
60
     * @return RedirectResponse
61
     */
62
    public function banAction(Node $node)
63
    {
64
        $this->checkPermission();
65
66
        /** @var NodeTranslation $nodeTranslation */
67
        foreach ($node->getNodeTranslations() as $nodeTranslation) {
68
            $route = $this->generateUrl('_slug', ['url' => $nodeTranslation->getUrl(), '_locale' => $nodeTranslation->getLang()]);
69
            $this->get('kunstmaan_cache.helper.varnish')->banPath($route);
70
        }
71
        $this->addFlash('success', 'kunstmaan_cache.varnish.ban.success');
72
73
        return $this->redirect($this->generateUrl('KunstmaanNodeBundle_nodes_edit', ['id' => $node->getId()]));
74
    }
75
76
    /**
77
     * Check permission
78
     *
79
     * @param string $roleToCheck
80
     *
81
     * @throws AccessDeniedException
82
     */
83
    private function checkPermission($roleToCheck = 'ROLE_SUPER_ADMIN')
84
    {
85
        if (false === $this->isGranted($roleToCheck)) {
86
            throw new AccessDeniedException();
87
        }
88
    }
89
}
90