Passed
Push — trunk ( 7ae588...5f8f01 )
by Christian
13:45 queued 12s
created

UpdateSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Theme\Subscriber;
4
5
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
6
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
7
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
8
use Shopware\Core\Framework\Log\Package;
9
use Shopware\Core\Framework\Plugin\PluginLifecycleService;
10
use Shopware\Core\Framework\Update\Event\UpdatePostFinishEvent;
11
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...
12
use Shopware\Storefront\Theme\Exception\ThemeCompileException;
13
use Shopware\Storefront\Theme\ThemeCollection;
14
use Shopware\Storefront\Theme\ThemeEntity;
0 ignored issues
show
Bug introduced by
The type Shopware\Storefront\Theme\ThemeEntity 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...
15
use Shopware\Storefront\Theme\ThemeLifecycleService;
16
use Shopware\Storefront\Theme\ThemeService;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19
/**
20
 * @internal
21
 */
22
#[Package('storefront')]
23
class UpdateSubscriber implements EventSubscriberInterface
24
{
25
    /**
26
     * @internal
27
     */
28
    public function __construct(
29
        private readonly ThemeService $themeService,
30
        private readonly ThemeLifecycleService $themeLifecycleService,
31
        private readonly EntityRepository $salesChannelRepository
32
    ) {
33
    }
34
35
    /**
36
     * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, string|arr...y{0: string, 1?: int}>> at position 17 could not be parsed: Expected '>' at position 17, but found 'list'.
Loading history...
37
     */
38
    public static function getSubscribedEvents(): array
39
    {
40
        return [
41
            UpdatePostFinishEvent::class => 'updateFinished',
42
        ];
43
    }
44
45
    /**
46
     * @internal
47
     */
48
    public function updateFinished(UpdatePostFinishEvent $event): void
49
    {
50
        $context = $event->getContext();
51
        $this->themeLifecycleService->refreshThemes($context);
52
53
        if ($context->hasState(PluginLifecycleService::STATE_SKIP_ASSET_BUILDING)) {
54
            return;
55
        }
56
57
        $criteria = new Criteria();
58
        $criteria->addFilter(new EqualsFilter('active', true));
59
        $criteria->getAssociation('themes')
60
            ->addFilter(new EqualsFilter('active', true));
61
62
        $alreadyCompiled = [];
63
        /** @var SalesChannelEntity $salesChannel */
64
        foreach ($this->salesChannelRepository->search($criteria, $context) as $salesChannel) {
65
            $themes = $salesChannel->getExtension('themes');
66
            if (!$themes instanceof ThemeCollection) {
67
                continue;
68
            }
69
70
            $failedThemes = [];
71
72
            /** @var ThemeEntity $theme */
73
            foreach ($themes as $theme) {
74
                // NEXT-21735 - his is covered randomly
75
                // @codeCoverageIgnoreStart
76
                if (\in_array($theme->getId(), $alreadyCompiled, true) !== false) {
77
                    continue;
78
                }
79
                // @codeCoverageIgnoreEnd
80
81
                try {
82
                    $alreadyCompiled += $this->themeService->compileThemeById($theme->getId(), $context);
83
                } catch (ThemeCompileException $e) {
84
                    $failedThemes[] = $theme->getName();
85
                    $alreadyCompiled[] = $theme->getId();
86
                }
87
            }
88
89
            if (!empty($failedThemes)) {
90
                $event->appendPostUpdateMessage('Theme(s): ' . implode(', ', $failedThemes) . ' could not be recompiled.');
91
            }
92
        }
93
    }
94
}
95