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

CacheWarmerTaskHandler::getHandledMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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\Adapter\Cache\CacheTagCollection;
7
use Shopware\Core\Framework\Routing\RequestTransformerInterface;
8
use Shopware\Core\Kernel;
9
use Shopware\Storefront\Framework\Cache\CacheStore;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
12
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
13
use Symfony\Component\Routing\RouterInterface;
14
15
/**
16
 * @package storefront
17
 *
18
 * @internal
19
 */
20
final class CacheWarmerTaskHandler implements MessageSubscriberInterface
21
{
22
    private Kernel $kernel;
23
24
    private RouterInterface $router;
25
26
    private RequestTransformerInterface $requestTransformer;
27
28
    private CacheIdLoader $cacheIdLoader;
29
30
    private CacheTagCollection $cacheTagCollection;
31
32
    public function __construct(Kernel $kernel, RouterInterface $router, RequestTransformerInterface $requestTransformer, CacheIdLoader $cacheIdLoader, CacheTagCollection $cacheTagCollection)
33
    {
34
        $this->kernel = $kernel;
35
        $this->router = $router;
36
        $this->requestTransformer = $requestTransformer;
37
        $this->cacheIdLoader = $cacheIdLoader;
38
        $this->cacheTagCollection = $cacheTagCollection;
39
    }
40
41
    public function __invoke(WarmUpMessage $message): void
42
    {
43
        if ($this->cacheIdLoader->load() !== $message->getCacheId()) {
44
            return;
45
        }
46
47
        $kernel = $this->createHttpCacheKernel($message->getCacheId());
48
49
        foreach ($message->getParameters() as $parameters) {
50
            $url = rtrim($message->getDomain(), '/') . $this->router->generate($message->getRoute(), $parameters);
51
52
            $request = $this->requestTransformer->transform(Request::create($url));
53
54
            $kernel->handle($request);
55
56
            // the cache tag collection, collects all cache tags for a single request,
57
            // after the request handled, the collection has to be reset for the next request
58
            $this->cacheTagCollection->reset();
59
        }
60
    }
61
62
    /**
63
     * @return iterable<string>
64
     */
65
    public static function getHandledMessages(): iterable
66
    {
67
        return [WarmUpMessage::class];
68
    }
69
70
    private function createHttpCacheKernel(string $cacheId): HttpCache
71
    {
72
        $this->kernel->reboot(null, null, $cacheId);
73
74
        $store = $this->kernel->getContainer()->get(CacheStore::class);
75
76
        return new HttpCache($this->kernel, $store, null);
0 ignored issues
show
Bug introduced by
It seems like $store can also be of type null; however, parameter $store of Symfony\Component\HttpKe...ttpCache::__construct() does only seem to accept Symfony\Component\HttpKe...ttpCache\StoreInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

76
        return new HttpCache($this->kernel, /** @scrutinizer ignore-type */ $store, null);
Loading history...
77
    }
78
}
79