Passed
Push — master ( e1c3af...d33dd4 )
by Mateusz
13:19 queued 07:37
created

RenderLinkRuntime::renderLinkForCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 10
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Twig\Runtime;
14
15
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
16
use Sylius\Component\Locale\Context\LocaleContextInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
use Twig\Environment;
19
20
final class RenderLinkRuntime implements RenderLinkRuntimeInterface
21
{
22
    /** @var LocaleContextInterface */
23
    private $localeContext;
24
25
    /** @var PageRepositoryInterface */
26
    private $pageRepository;
27
28
    /** @var RouterInterface */
29
    private $router;
30
31
    /** @var string */
32
    private $defaultTemplate;
33
34
    public function __construct(
35
        LocaleContextInterface $localeContext,
36
        PageRepositoryInterface $pageRepository,
37
        RouterInterface $router,
38
        string $defaultTemplate
39
    ) {
40
        $this->localeContext = $localeContext;
41
        $this->pageRepository = $pageRepository;
42
        $this->router = $router;
43
        $this->defaultTemplate = $defaultTemplate;
44
    }
45
46
    public function renderLinkForCode(
47
        Environment $environment,
48
        string $code,
49
        array $options = [],
50
        ?string $template = null
51
    ): string {
52
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
53
54
        return $environment->render($template ?? $this->defaultTemplate, [
55
            'page' => $page,
56
            'options' => $options,
57
        ]);
58
    }
59
60
    public function getLinkForCode(
61
        string $code,
62
        array $options = []
63
    ): string {
64
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
65
        if (!$page) {
66
            return $options['notFoundMessage'] ?? '';
67
        }
68
69
        return $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', ['slug' => $page->getSlug()]);
70
    }
71
}
72