Passed
Push — trunk ( 37af04...c71cc1 )
by Christian
13:15 queued 12s
created

CacheWarmer::createHttpCacheKernel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Storefront\Framework\Cache\CacheWarmer;
4
5
use Shopware\Core\Framework\Adapter\Cache\CacheIdLoader;
6
use Shopware\Core\Framework\Context;
7
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
8
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
9
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
10
use Shopware\Core\Framework\Feature;
11
use Shopware\Core\Framework\MessageQueue\Handler\AbstractMessageHandler;
12
use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelDomain\SalesChannelDomainEntity;
13
use Symfony\Component\Messenger\MessageBusInterface;
14
15
/**
16
 * @package storefront
17
 *
18
 * @deprecated tag:v6.5.0 - reason:class-hierarchy-change - Won't extend AbstractMessageHandler anymore, message handling is done in `CacheWarmerTaskHandler`
19
 */
20
class CacheWarmer extends AbstractMessageHandler
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\Framework\...\AbstractMessageHandler has been deprecated: tag:v6.5.0 - reason:reason:class-hierarchy-change - will be removed, use default symfony MessageSubscriberInterface instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

20
class CacheWarmer extends /** @scrutinizer ignore-deprecated */ AbstractMessageHandler
Loading history...
21
{
22
    private EntityRepositoryInterface $domainRepository;
23
24
    private MessageBusInterface $bus;
25
26
    private CacheRouteWarmerRegistry $registry;
27
28
    private CacheIdLoader $cacheIdLoader;
29
30
    private CacheWarmerTaskHandler $cacheWarmerTaskHandler;
31
32
    /**
33
     * @internal
34
     */
35
    public function __construct(
36
        EntityRepositoryInterface $domainRepository,
37
        MessageBusInterface $bus,
38
        CacheRouteWarmerRegistry $registry,
39
        CacheIdLoader $cacheIdLoader,
40
        CacheWarmerTaskHandler $cacheWarmerTaskHandler
41
    ) {
42
        $this->domainRepository = $domainRepository;
43
        $this->bus = $bus;
44
        $this->registry = $registry;
45
        $this->cacheIdLoader = $cacheIdLoader;
46
        $this->cacheWarmerTaskHandler = $cacheWarmerTaskHandler;
47
    }
48
49
    /**
50
     * @deprecated tag:v6.5.0 - reason:remove-subscriber - will be removed, use `CacheWarmerTaskHandler` instead
51
     *
52
     * @return iterable<string>
53
     */
54
    public static function getHandledMessages(): iterable
55
    {
56
        return [];
57
    }
58
59
    public function warmUp(?string $cacheId = null): void
60
    {
61
        $cacheId = $cacheId ?? $this->cacheIdLoader->load();
62
63
        $criteria = new Criteria();
64
        $domains = $this->domainRepository->search($criteria, Context::createDefaultContext());
65
66
        $this->cacheIdLoader->write($cacheId);
67
68
        // generate all message to calculate message count
69
        $this->createMessages($cacheId, $domains);
70
    }
71
72
    /**
73
     * @deprecated tag:v6.5.0 - will be removed, use `CacheWarmerTaskHandler` instead
74
     *
75
     * @param object $message
76
     */
77
    public function handle($message): void
78
    {
79
        Feature::triggerDeprecationOrThrow(
80
            'v6.5.0.0',
81
            Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0', CacheWarmerTaskHandler::class)
82
        );
83
84
        if (!$message instanceof WarmUpMessage) {
85
            return;
86
        }
87
88
        $this->cacheWarmerTaskHandler->__invoke($message);
89
    }
90
91
    private function createMessages(string $cacheId, EntitySearchResult $domains): void
92
    {
93
        /** @var SalesChannelDomainEntity $domain */
94
        foreach ($domains as $domain) {
95
            foreach ($this->registry->getWarmers() as $warmer) {
96
                $message = $warmer->createMessage($domain, null);
0 ignored issues
show
Bug introduced by
$domain of type array is incompatible with the type Shopware\Core\System\Sal...alesChannelDomainEntity expected by parameter $domain of Shopware\Storefront\Fram...Warmer::createMessage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
                $message = $warmer->createMessage(/** @scrutinizer ignore-type */ $domain, null);
Loading history...
97
98
                while ($message) {
99
                    $offset = $message->getOffset();
100
101
                    $message->setCacheId($cacheId);
102
                    $message->setDomain($domain->getUrl());
103
104
                    $this->bus->dispatch($message);
105
106
                    $message = $warmer->createMessage($domain, $offset);
107
                }
108
            }
109
        }
110
    }
111
}
112