Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
dl 0
loc 103
ccs 0
cts 38
cp 0
rs 10
c 1
b 0
f 0
wmc 23

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkIfUriIsCanonical() 0 13 3
B feed() 0 25 7
B show() 0 40 10
A preview() 0 15 3
1
<?php
2
3
namespace PiedWeb\CMSBundle\Controller;
4
5
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Translation\TranslatorInterface;
11
12
class PageController extends AbstractController
13
{
14
    protected $translator;
15
16
    public function show(
17
        ?string $slug,
18
        Request $request,
19
        TranslatorInterface $translator,
20
        ParameterBagInterface $params
21
    ) {
22
        $slug = (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
23
        $page = $this->getDoctrine()
24
            ->getRepository($this->container->getParameter('app.entity_page'))
1 ignored issue
show
Bug introduced by
The method getParameter() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as Symfony\Component\Depend...tion\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            ->getRepository($this->container->/** @scrutinizer ignore-call */ getParameter('app.entity_page'))
Loading history...
25
            ->findOneBySlug($slug);
1 ignored issue
show
Bug introduced by
The method findOneBySlug() does not exist on Doctrine\Persistence\ObjectRepository. Did you maybe mean findOneBy()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            ->/** @scrutinizer ignore-call */ findOneBySlug($slug);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
27
        // Check if page exist
28
        if (null === $page) {
29
            throw $this->createNotFoundException();
30
        }
31
32
        if (null !== $page->getLocale()) { // avoid bc break, todo remove it
33
            $request->setLocale($page->getLocale());
34
        }
35
36
        // Check if page is public
37
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
38
            throw $this->createNotFoundException();
39
        }
40
41
        // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada)
42
        $redirect = $this->checkIfUriIsCanonical($request, $page);
43
        if (false !== $redirect) {
44
            return $this->redirect($redirect[0], $redirect[1]);
45
        }
46
47
        // Maybe the page is a redirection
48
        if (false !== $page->getRedirection()) {
49
            return $this->redirect($page->getRedirection(), $page->getRedirectionCode());
50
        }
51
52
        // method_exists($this->container->getParameter('app.entity_page'), 'getTemplate') &&
53
        $template = null !== $page->getTemplate() ? $page->getTemplate() : $params->get('app.default_page_template');
54
55
        return $this->render($template, ['page' => $page]);
56
    }
57
58
    protected function checkIfUriIsCanonical($request, $page)
59
    {
60
        $real = $request->getRequestUri();
61
62
        $expected = 'homepage' == $page->getSlug() ?
63
            $this->get('piedweb.page_canonical')->generatePathForHomepage() :
64
            $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug());
65
66
        if ($real != $expected) {
67
            return [$request->getBasePath().$expected, 301];
68
        }
69
70
        return false;
71
    }
72
73
    public function preview(
74
        ?string $slug,
75
        Request $request
76
    ) {
77
        $pageEntity = $this->container->getParameter('app.entity_page');
78
79
        $page = (null === $slug || '' === $slug) ?
80
            new $pageEntity()
81
            : $this->getDoctrine()
82
            ->getRepository($pageEntity)
83
            ->findOneBySlug(rtrim(strtolower($slug), '/'));
84
85
        $plainText = \PiedWeb\CMSBundle\Twig\AppExtension::convertMarkdownImage($request->request->get('plaintext'));
86
87
        return $this->render('@PiedWebCMS/admin/preview.html.twig', ['page' => $page, 'plainText' => $plainText]);
88
    }
89
90
    public function feed(
91
        ?string $slug,
92
        Request $request,
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
        /** @scrutinizer ignore-unused */ Request $request,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
        TranslatorInterface $translator,
0 ignored issues
show
Unused Code introduced by
The parameter $translator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
        /** @scrutinizer ignore-unused */ TranslatorInterface $translator,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
        ParameterBagInterface $params
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

94
        /** @scrutinizer ignore-unused */ ParameterBagInterface $params

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    ) {
96
        $slug = (null === $slug || '' === $slug) ? 'homepage' : rtrim(strtolower($slug), '/');
97
        $page = $this->getDoctrine()
98
            ->getRepository($this->container->getParameter('app.entity_page'))
99
            ->findOneBySlug($slug);
100
101
        // Check if page exist
102
        if (null === $page || $page->getChildrenPages()->count() < 1) {
103
            throw $this->createNotFoundException();
104
        }
105
106
        // Check if page is public
107
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
108
            throw $this->createNotFoundException();
109
        }
110
111
        $response = new Response();
112
        $response->headers->set('Content-Type', 'text/xml');
113
114
        return $this->render('@PiedWebCMS/page/rss.xml.twig', ['page' => $page], $response);
115
    }
116
}
117