Completed
Push — master ( 6beba2...e1e95a )
by Dev
03:27
created

PageController::showList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 3
1
<?php
2
3
namespace PiedWeb\CMSBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\Translation\TranslatorInterface;
8
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
9
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
10
11
class PageController extends AbstractController
12
{
13
    protected $translator;
14
15
    public function show(string $slug = 'homepage', Request $request, TranslatorInterface $translator, ParameterBagInterface $params)
16
    {
17
        $slug = '' == $slug ? 'homepage' : rtrim(strtolower($slug), '/');
18
        $page = $this->getDoctrine()->getRepository($this->container->getParameter('app.entity_page'))->findOneBySlug($slug, $request->getLocale());
0 ignored issues
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

18
        $page = $this->getDoctrine()->getRepository($this->container->/** @scrutinizer ignore-call */ getParameter('app.entity_page'))->findOneBySlug($slug, $request->getLocale());
Loading history...
Bug introduced by
The method findOneBySlug() does not exist on Doctrine\Common\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

18
        $page = $this->getDoctrine()->getRepository($this->container->getParameter('app.entity_page'))->/** @scrutinizer ignore-call */ findOneBySlug($slug, $request->getLocale());

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...
19
20
        // Check if page exist
21
        if (null === $page) {
22
            throw $this->createNotFoundException();
23
        }
24
25
        // Check if page is public
26
        if ($page->getCreatedAt() > new \DateTimeImmutable() && !$this->isGranted('ROLE_ADMIN')) {
27
            throw $this->createNotFoundException();
28
        }
29
30
        // SEO redirection if we are not on the good URI (for exemple /fr/tagada instead of /tagada)
31
        $redirect = $this->checkIfUriIsCanonical($request, $page);
32
        if (false !== $redirect) {
33
            return $this->redirect($redirect[0], $redirect[1]);
34
        }
35
36
        // Maybe the page is a redirection
37
        if (false !== $page->getRedirection()) {
38
            return $this->redirect($page->getRedirection(), $page->getRedirectionCode());
39
        }
40
41
        $template = method_exists($this->container->getParameter('app.entity_page'), 'getTemplate') && null !== $page->getTemplate() ? $page->getTemplate() : $params->get('app.default_page_template');
42
43
        return $this->render($template, ['page' => $page]);
44
    }
45
46
    protected function checkIfUriIsCanonical($request, $page)
47
    {
48
        $real = $request->getRequestUri();
49
50
        $defaultLocale = $this->container->getParameter('locale');
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultLocale is dead and can be removed.
Loading history...
51
52
        $expected = 'homepage' == $page->getSlug() ?
53
            $this->get('piedweb.page_canonical')->generatePathForHomepage() :
54
            $this->get('piedweb.page_canonical')->generatePathForPage($page->getRealSlug())
55
        ;
56
57
        if ($real != $expected) {
58
            return [$request->getBasePath().$expected, 301];
59
            // may log ?
60
        }
61
62
        return false;
63
    }
64
}
65