Completed
Pull Request — master (#169)
by Mikołaj
02:27
created

RenderProductPagesExtension::sortBySections()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
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.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Twig\Extension;
14
15
use BitBag\SyliusCmsPlugin\Entity\PageContentInterface;
16
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
17
use Sylius\Component\Channel\Context\ChannelContextInterface;
18
use Sylius\Component\Core\Model\ProductInterface;
19
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
20
21
final class RenderProductPagesExtension extends \Twig_Extension
22
{
23
    /** @var PageRepositoryInterface */
24
    private $pageRepository;
25
26
    /** @var ChannelContextInterface */
27
    private $channelContext;
28
29
    /** @var EngineInterface */
30
    private $templatingEngine;
31
32
    public function __construct(
33
        PageRepositoryInterface $pageRepository,
34
        ChannelContextInterface $channelContext,
35
        EngineInterface $templatingEngine
36
    ) {
37
        $this->pageRepository = $pageRepository;
38
        $this->channelContext = $channelContext;
39
        $this->templatingEngine = $templatingEngine;
40
    }
41
42
    public function getFunctions(): array
43
    {
44
        return [
45
            new \Twig_Function('bitbag_cms_render_product_pages', [$this, 'renderProductPages'], ['is_safe' => ['html']]),
46
        ];
47
    }
48
49
    public function renderProductPages(ProductInterface $product, string $sectionCode = null): string
50
    {
51
        $channelCode = $this->channelContext->getChannel()->getCode();
52
53
        if (null !== $sectionCode) {
54
            $pages = $this->pageRepository->findByProductAndSectionCode($product, $sectionCode, $channelCode);
55
        } else {
56
            $pages = $this->pageRepository->findByProduct($product, $channelCode);
57
        }
58
59
        $data = $this->sortBySections($pages);
60
61
        return $this->templatingEngine->render('@BitBagSyliusCmsPlugin/Shop/Product/_pagesBySection.html.twig', [
62
            'data' => $data,
63
        ]);
64
    }
65
66
    private function sortBySections(array $pages): array
67
    {
68
        $result = [];
69
70
        /** @var PageContentInterface $page */
71
        foreach ($pages as $page) {
72
            foreach ($page->getSections() as $section) {
73
                $sectionCode = $section->getCode();
74
                if (!array_key_exists($sectionCode, $result)) {
75
                    $result[$sectionCode] = [];
76
                    $result[$sectionCode]['section'] = $section;
77
                }
78
79
                $result[$sectionCode][] = $page;
80
            }
81
        }
82
83
        return $result;
84
    }
85
}
86