Passed
Push — trunk ( 17375e...d9bd07 )
by Christian
12:35 queued 12s
created

CompileThemeHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 5
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Theme\Message;
4
5
use Shopware\Administration\Notification\NotificationService;
6
use Shopware\Core\Framework\Context;
7
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
8
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
9
use Shopware\Core\Framework\Log\Package;
10
use Shopware\Core\Framework\Uuid\Uuid;
11
use Shopware\Core\System\SalesChannel\SalesChannelEntity;
12
use Shopware\Storefront\Theme\ConfigLoader\AbstractConfigLoader;
13
use Shopware\Storefront\Theme\Exception\ThemeException;
14
use Shopware\Storefront\Theme\StorefrontPluginRegistryInterface;
15
use Shopware\Storefront\Theme\ThemeCompiler;
16
use Shopware\Storefront\Theme\ThemeService;
17
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
18
19
/**
20
 * @internal
21
 */
22
#[AsMessageHandler]
23
#[Package('storefront')]
24
final class CompileThemeHandler
25
{
26
    public function __construct(
27
        private readonly ThemeCompiler $themeCompiler,
28
        private readonly AbstractConfigLoader $configLoader,
29
        private readonly StorefrontPluginRegistryInterface $extensionRegistry,
30
        private readonly NotificationService $notificationService,
31
        private readonly EntityRepository $saleschannelRepository
32
    ) {
33
    }
34
35
    public function __invoke(CompileThemeMessage $message): void
36
    {
37
        $message->getContext()->addState(ThemeService::STATE_NO_QUEUE);
38
        $this->themeCompiler->compileTheme(
39
            $message->getSalesChannelId(),
40
            $message->getThemeId(),
41
            $this->configLoader->load($message->getThemeId(), $message->getContext()),
42
            $this->extensionRegistry->getConfigurations(),
43
            $message->isWithAssets(),
44
            $message->getContext()
45
        );
46
47
        if ($message->getContext()->getScope() !== Context::USER_SCOPE) {
48
            return;
49
        }
50
        /** @var SalesChannelEntity|null $salesChannel */
51
        $salesChannel = $this->saleschannelRepository->search(
52
            new Criteria([$message->getSalesChannelId()]),
53
            $message->getContext()
54
        )->first();
55
56
        if ($salesChannel === null) {
57
            throw ThemeException::salesChannelNotFound($message->getSalesChannelId());
58
        }
59
60
        $this->notificationService->createNotification(
61
            [
62
                'id' => Uuid::randomHex(),
63
                'status' => 'info',
64
                'message' => 'Compilation for sales channel ' . $salesChannel->getName() . ' completed',
65
                'requiredPrivileges' => [],
66
            ],
67
            $message->getContext()
68
        );
69
    }
70
}
71