Passed
Push — trunk ( dee18d...92a8fe )
by Christian
12:13 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;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKernel\HttpCache\HttpCache 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 Symfony\Component\Messenger\Attribute\AsMessageHandler;
13
use Symfony\Component\Routing\RouterInterface;
14
15
/**
16
 * @package storefront
17
 *
18
 * @internal
19
 */
20
#[AsMessageHandler]
21
final class CacheWarmerTaskHandler
22
{
23
    private Kernel $kernel;
24
25
    private RouterInterface $router;
26
27
    private RequestTransformerInterface $requestTransformer;
28
29
    private CacheIdLoader $cacheIdLoader;
30
31
    private CacheTagCollection $cacheTagCollection;
32
33
    public function __construct(Kernel $kernel, RouterInterface $router, RequestTransformerInterface $requestTransformer, CacheIdLoader $cacheIdLoader, CacheTagCollection $cacheTagCollection)
34
    {
35
        $this->kernel = $kernel;
36
        $this->router = $router;
37
        $this->requestTransformer = $requestTransformer;
38
        $this->cacheIdLoader = $cacheIdLoader;
39
        $this->cacheTagCollection = $cacheTagCollection;
40
    }
41
42
    public function __invoke(WarmUpMessage $message): void
43
    {
44
        if ($this->cacheIdLoader->load() !== $message->getCacheId()) {
45
            return;
46
        }
47
48
        $kernel = $this->createHttpCacheKernel($message->getCacheId());
49
50
        foreach ($message->getParameters() as $parameters) {
51
            $url = rtrim($message->getDomain(), '/') . $this->router->generate($message->getRoute(), $parameters);
52
53
            $request = $this->requestTransformer->transform(Request::create($url));
54
55
            $kernel->handle($request);
56
57
            // the cache tag collection, collects all cache tags for a single request,
58
            // after the request handled, the collection has to be reset for the next request
59
            $this->cacheTagCollection->reset();
60
        }
61
    }
62
63
    private function createHttpCacheKernel(string $cacheId): HttpCache
64
    {
65
        $this->kernel->reboot(null, null, $cacheId);
66
67
        $store = $this->kernel->getContainer()->get(CacheStore::class);
68
69
        return new HttpCache($this->kernel, $store, null);
70
    }
71
}
72