Completed
Push — master ( a7fff6...5ea170 )
by Jeroen
18:57 queued 10s
created

VarnishController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 72
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 22 3
A banAction() 0 13 2
A checkPermission() 0 6 2
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 "Symfony\Bundle\FrameworkBundle\Controller\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
0 ignored issues
show
Documentation introduced by
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