Passed
Pull Request — master (#310)
by Vojtěch
10:41
created

RenderSectionPagesExtension::renderSectionPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusCmsPlugin\Twig\Extension;
6
7
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
8
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
9
use Sylius\Component\Channel\Context\ChannelContextInterface;
10
use Sylius\Component\Locale\Context\LocaleContextInterface;
11
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
12
use Twig\Extension\AbstractExtension;
13
14
final class RenderSectionPagesExtension extends AbstractExtension
15
{
16
    /** @var PageRepositoryInterface */
17
    private $pageRepository;
18
19
    /** @var SectionRepositoryInterface */
20
    private $sectionRepository;
21
22
    /** @var ChannelContextInterface */
23
    private $channelContext;
24
25
    /** @var LocaleContextInterface */
26
    private $localeContext;
27
28
    /** @var EngineInterface */
29
    private $templatingEngine;
30
31
    public function __construct(
32
        PageRepositoryInterface $pageRepository,
33
        SectionRepositoryInterface $sectionRepository,
34
        ChannelContextInterface $channelContext,
35
        LocaleContextInterface $localeContext,
36
        EngineInterface $templatingEngine
37
    ) {
38
        $this->pageRepository = $pageRepository;
39
        $this->sectionRepository = $sectionRepository;
40
        $this->channelContext = $channelContext;
41
        $this->localeContext = $localeContext;
42
        $this->templatingEngine = $templatingEngine;
43
    }
44
45
    public function getFunctions(): array
46
    {
47
        return [
48
            new \Twig_Function('bitbag_cms_render_section_pages', [$this, 'renderSectionPages'], ['is_safe' => ['html']]),
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_render_section_pages', [$this, 'renderSectionPages'], ['is_safe' => ['html']]),
Loading history...
49
        ];
50
    }
51
52
    public function renderSectionPages(string $sectionCode, ?string $template = null): string
53
    {
54
        $channelCode = $this->channelContext->getChannel()->getCode();
55
        $section = $this->sectionRepository->findOneByCode($sectionCode, $this->localeContext->getLocaleCode());
56
        $pages = $this->pageRepository->createShopListQueryBuilder($sectionCode, $channelCode)->getQuery()->getResult();
0 ignored issues
show
Bug introduced by
It seems like $channelCode can also be of type null; however, parameter $channelCode of BitBag\SyliusCmsPlugin\R...eShopListQueryBuilder() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        $pages = $this->pageRepository->createShopListQueryBuilder($sectionCode, /** @scrutinizer ignore-type */ $channelCode)->getQuery()->getResult();
Loading history...
57
58
        return $this->templatingEngine->render($template ?? '@BitBagSyliusCmsPlugin/Shop/Section/_pagesBySection.html.twig', [
59
            'section' => $section,
60
            'pages' => $pages,
61
        ]);
62
    }
63
}
64