Completed
Push — master ( a7fff6...5ea170 )
by Jeroen
18:57 queued 10s
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
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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
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