RenderLinkRuntime::getLinkForCode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Twig\Runtime;
12
13
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
14
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
15
use Sylius\Component\Locale\Context\LocaleContextInterface;
16
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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
        /** @var PageInterface|null $page */
65
        $page = $this->pageRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
66
        if (null === $page) {
67
            throw new NotFoundHttpException('Page for code "' . $code . '" not found');
68
        }
69
70
        return $this->router->generate('bitbag_sylius_cms_plugin_shop_page_show', ['slug' => $page->getSlug()]);
71
    }
72
}
73