Completed
Push — master ( b8bb13...9508d6 )
by US
23s queued 11s
created

RenderLinkExtension::getLinkForCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 10
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
/*
4
 * Created by Florian Merle - Dedi Agency <[email protected]> <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace BitBag\SyliusCmsPlugin\Twig\Extension;
10
11
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
12
use Sylius\Component\Locale\Context\LocaleContextInterface;
13
use Symfony\Component\Routing\RouterInterface;
14
15
class RenderLinkExtension extends \Twig_Extension
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Extension has been deprecated: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead ( Ignorable by Annotation )

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

15
class RenderLinkExtension extends /** @scrutinizer ignore-deprecated */ \Twig_Extension
Loading history...
16
{
17
    /** @var LocaleContextInterface */
18
    private $localeContext;
19
20
    /** @var PageRepositoryInterface */
21
    private $pageRepository;
22
23
    /** @var RouterInterface */
24
    private $router;
25
26
    /** @var string */
27
    private $defaultTemplate;
28
29
    public function __construct(
30
        LocaleContextInterface $localeContext,
31
        PageRepositoryInterface $pageRepository,
32
        RouterInterface $router,
33
        string $defaultTemplate
34
    ) {
35
        $this->localeContext = $localeContext;
36
        $this->pageRepository = $pageRepository;
37
        $this->router = $router;
38
        $this->defaultTemplate = $defaultTemplate;
39
    }
40
41
    public function getFunctions()
42
    {
43
        return [
44
            new \Twig_Function('bitbag_cms_render_link_for_code', [$this, 'renderLinkForCode'], [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

44
            /** @scrutinizer ignore-deprecated */ new \Twig_Function('bitbag_cms_render_link_for_code', [$this, 'renderLinkForCode'], [
Loading history...
45
                'needs_environment' => true,
46
                'is_safe' => ['html']
47
            ]),
48
            new \Twig_Function('bitbag_cms_get_link_for_code', [$this, 'getLinkForCode']),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Function has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead ( Ignorable by Annotation )

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

48
            /** @scrutinizer ignore-deprecated */ new \Twig_Function('bitbag_cms_get_link_for_code', [$this, 'getLinkForCode']),
Loading history...
49
        ];
50
    }
51
52
    public function renderLinkForCode(
53
        \Twig_Environment $environment,
54
        string $code,
55
        array $options = [],
56
        ?string $template = null
57
    ): string {
58
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
59
60
        return $environment->render($template ?? $this->defaultTemplate, [
61
            'page' => $page,
62
            'options' => $options,
63
        ]);
64
    }
65
66
    public function getLinkForCode(
67
        string $code,
68
        array $options = []
69
    ): string {
70
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
71
        if (!$page) {
72
            return isset($options['notFoundMessage']) ? $options['notFoundMessage'] : '';
73
        }
74
75
        return $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', ['slug' => $page->getSlug()]);
76
    }
77
}
78