DefaultController::showAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Beelab\SimplePageBundle\Controller;
4
5
use Beelab\SimplePageBundle\Util\BreadCrumbs;
6
use Doctrine\Persistence\ManagerRegistry;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Twig\Environment;
10
11
class DefaultController
12
{
13
    /**
14
     * @var ManagerRegistry
15
     */
16
    protected $doctrine;
17
18
    /**
19
     * @var Environment
20
     */
21
    protected $twig;
22
23
    /**
24
     * @var string
25
     */
26
    protected $entity;
27
28
    /**
29
     * @var string
30
     */
31
    protected $prefix;
32
33
    public function __construct(ManagerRegistry $doctrine, Environment $twig, string $entity, string $prefix)
34
    {
35
        $this->doctrine = $doctrine;
36
        $this->twig = $twig;
37
        $this->entity = $entity;
38
        $this->prefix = $prefix;
39
    }
40
41
    /**
42
     * You must define a final route in your configuration, pointing to this action.
43
     */
44
    public function showAction(string $path = ''): Response
45
    {
46
        $page = $this->doctrine->getRepository($this->entity)->findOneByPath($path);
0 ignored issues
show
Bug introduced by
The method findOneByPath() does not exist on Doctrine\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
        if (empty($page)) {
48
            throw new NotFoundHttpException(\sprintf('Page not found for path "/%s".', $path));
49
        }
50
        $breadCrumbs = BreadCrumbs::create($path);
51
        $template = $this->prefix.\str_replace(' ', '_', $page->getTemplate()).'.html.twig';
52
53
        return new Response($this->twig->render($template, ['page' => $page, 'breadCrumbs' => $breadCrumbs]));
54
    }
55
}
56