Passed
Push — trunk ( c406d2...938deb )
by Christian
10:25 queued 12s
created

DatabaseAvailableThemeProvider::load()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 6
eloc 20
nc 12
nop 2
dl 0
loc 38
rs 8.9777
c 1
b 1
f 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shopware\Storefront\Theme\ConfigLoader;
5
6
use Shopware\Core\Defaults;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
10
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
11
use Shopware\Core\Framework\Feature;
12
use Shopware\Core\Framework\Log\Package;
13
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
14
use Shopware\Core\System\SalesChannel\SalesChannelCollection;
15
use Shopware\Storefront\Theme\ThemeCollection;
16
17
#[Package('storefront')]
18
class DatabaseAvailableThemeProvider extends AbstractAvailableThemeProvider
19
{
20
    /**
21
     * @internal
22
     */
23
    public function __construct(private readonly EntityRepository $salesChannelRepository)
24
    {
25
    }
26
27
    public function getDecorated(): AbstractAvailableThemeProvider
28
    {
29
        throw new DecorationPatternException(self::class);
30
    }
31
32
    /**
33
     * @deprecated tag:v6.6.0 - Second parameter $activeOnly will be required in future versions.
34
     */
35
    public function load(Context $context, bool $activeOnly = false): array
36
    {
37
        if (\count(\func_get_args()) === 1) {
38
            Feature::triggerDeprecationOrThrow(
39
                'v6_6_0_0',
40
                sprintf(
41
                    'Method %s::%s is deprecated. Second parameter $activeOnly will be required in future versions.',
42
                    __CLASS__,
43
                    __METHOD__,
44
                )
45
            );
46
        }
47
48
        $criteria = new Criteria();
49
        $criteria->addFilter(new EqualsFilter('typeId', Defaults::SALES_CHANNEL_TYPE_STOREFRONT));
50
51
        if ($activeOnly) {
52
            $criteria->addFilter(new EqualsFilter('active', 1));
53
        }
54
55
        $criteria->addAssociation('themes');
56
57
        /** @var SalesChannelCollection $result */
58
        $result = $this->salesChannelRepository->search($criteria, $context)->getEntities();
59
60
        $list = [];
61
62
        foreach ($result->getElements() as $salesChannel) {
63
            /** @var ThemeCollection|null $themes */
64
            $themes = $salesChannel->getExtensionOfType('themes', ThemeCollection::class);
65
            if (!$themes || !$theme = $themes->first()) {
66
                continue;
67
            }
68
69
            $list[$salesChannel->getId()] = $theme->getId();
70
        }
71
72
        return $list;
73
    }
74
}
75