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