Passed
Push — trunk ( f40966...f98169 )
by Christian
12:49 queued 14s
created

ConfigExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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