Passed
Push — trunk ( dd8a19...7fc211 )
by Christian
14:51 queued 18s
created

ConfigExtension::getSalesChannelId()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 7
nop 1
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Framework\Twig\Extension;
4
5
use Shopware\Core\System\SalesChannel\SalesChannelContext;
6
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
7
use Shopware\Storefront\Framework\Twig\TemplateConfigAccessor;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFunction;
10
11
/**
12
 * @package storefront
13
 */
14
class ConfigExtension extends AbstractExtension
15
{
16
    /**
17
     * @internal
18
     */
19
    public function __construct(private readonly TemplateConfigAccessor $config)
20
    {
21
    }
22
23
    public function getFunctions(): array
24
    {
25
        return [
26
            new TwigFunction('config', $this->config(...), ['needs_context' => true]),
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 26 at column 56
Loading history...
27
            new TwigFunction('theme_config', $this->theme(...), ['needs_context' => true]),
28
        ];
29
    }
30
31
    /**
32
     * @return string|bool|array|float|int|null
33
     */
34
    public function config(array $context, string $key)
35
    {
36
        return $this->config->config($key, $this->getSalesChannelId($context));
37
    }
38
39
    /**
40
     * @return string|bool|array|float|int|null
41
     */
42
    public function theme(array $context, string $key)
43
    {
44
        return $this->config->theme($key, $this->getContext($context), $this->getThemeId($context));
45
    }
46
47
    private function getSalesChannelId(array $context): ?string
48
    {
49
        if (isset($context['context'])) {
50
            $salesChannelContext = $context['context'];
51
            if ($salesChannelContext instanceof SalesChannelContext) {
52
                return $salesChannelContext->getSalesChannelId();
53
            }
54
        }
55
        if (isset($context['salesChannel'])) {
56
            $salesChannel = $context['salesChannel'];
57
            if ($salesChannel instanceof SalesChannelEntity) {
58
                return $salesChannel->getId();
59
            }
60
        }
61
62
        return null;
63
    }
64
65
    private function getThemeId(array $context): ?string
66
    {
67
        return $context['themeId'] ?? null;
68
    }
69
70
    private function getContext(array $context): SalesChannelContext
71
    {
72
        if (!isset($context['context'])) {
73
            throw new \RuntimeException('Missing sales channel context object');
74
        }
75
76
        $context = $context['context'];
77
78
        if (!$context instanceof SalesChannelContext) {
79
            throw new \RuntimeException('Missing sales channel context object');
80
        }
81
82
        return $context;
83
    }
84
}
85