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

RenderProductPagesExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 3
c 2
b 1
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
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\Twig\Runtime\RenderProductPagesRuntime;
16
use Twig\Extension\AbstractExtension;
17
use Twig\TwigFunction;
18
19
final class RenderProductPagesExtension extends AbstractExtension
20
{
21
    public function getFunctions(): array
22
    {
23
        return [
24
            new TwigFunction('bitbag_cms_render_product_pages', [RenderProductPagesRuntime::class, 'renderProductPages'], ['is_safe' => ['html']]),
25
        ];
26
    }
27
28
    public function renderProductPages(ProductInterface $product, ?string $sectionCode = null, ?string $date = null): string
0 ignored issues
show
Bug introduced by
The type BitBag\SyliusCmsPlugin\T...ension\ProductInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
    {
30
        $channelCode = $this->channelContext->getChannel()->getCode();
0 ignored issues
show
Bug Best Practice introduced by
The property channelContext does not exist on BitBag\SyliusCmsPlugin\T...erProductPagesExtension. Did you maybe forget to declare it?
Loading history...
31
32
        $parsedDate = null;
33
        if (!empty($date)) {
34
            $parsedDate = new \DateTimeImmutable($date);
35
        }
36
37
        if (null !== $sectionCode) {
38
            $pages = $this->pageRepository->findByProductAndSectionCode($product, $sectionCode, $channelCode, $parsedDate);
0 ignored issues
show
Bug Best Practice introduced by
The property pageRepository does not exist on BitBag\SyliusCmsPlugin\T...erProductPagesExtension. Did you maybe forget to declare it?
Loading history...
39
        } else {
40
            $pages = $this->pageRepository->findByProduct($product, $channelCode, $parsedDate);
41
        }
42
43
        $data = $this->sortBySections($pages);
44
45
        return $this->templatingEngine->render('@BitBagSyliusCmsPlugin/Shop/Product/_pagesBySection.html.twig', [
0 ignored issues
show
Bug Best Practice introduced by
The property templatingEngine does not exist on BitBag\SyliusCmsPlugin\T...erProductPagesExtension. Did you maybe forget to declare it?
Loading history...
46
            'data' => $data,
47
        ]);
48
    }
49
50
    private function sortBySections(array $pages): array
51
    {
52
        $result = [];
53
54
        /** @var PageInterface $page */
55
        foreach ($pages as $page) {
56
            foreach ($page->getSections() as $section) {
57
                $sectionCode = $section->getCode();
58
                if (!array_key_exists($sectionCode, $result)) {
59
                    $result[$sectionCode] = [];
60
                    $result[$sectionCode]['section'] = $section;
61
                }
62
63
                $result[$sectionCode][] = $page;
64
            }
65
        }
66
67
        return $result;
68
    }
69
}
70