Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.86% covered (danger)
42.86%
6 / 14
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DoCacheWarmupForApieMaker
42.86% covered (danger)
42.86%
6 / 14
66.67% covered (warning)
66.67%
2 / 3
6.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSubscribedEvents
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 onResourceCreated
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
4.05
1<?php
2namespace Apie\Maker\EventListeners;
3
4use Apie\Common\Events\ApieResourceCreated;
5use Apie\Maker\BoundedContext\Resources\CodeGeneratedLog;
6use Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand;
7use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
10use Symfony\Component\HttpKernel\Kernel;
11use Symfony\Component\HttpKernel\KernelEvents;
12
13final class DoCacheWarmupForApieMaker implements EventSubscriberInterface
14{
15    public function __construct(
16        private readonly CacheWarmerAggregate $cacheWarmer,
17        private readonly Kernel $kernel
18    ) {
19    }
20    /**
21     * @return array<string, array<int, string>>
22     */
23    public static function getSubscribedEvents(): array
24    {
25        return [
26            ApieResourceCreated::class => ['onResourceCreated'],
27        ];
28    }
29
30    /**
31     * Code is similar to the code in CacheWarmupCommand
32     *
33     * @see CacheWarmupCommand::execute
34     */
35    public function onResourceCreated(ApieResourceCreated $resourceCreated): void
36    {
37        if (!($resourceCreated->resource instanceof CodeGeneratedLog)) {
38            return;
39        }
40        $cacheDir = $this->kernel->getContainer()->getParameter('kernel.cache_dir');
41
42        /** @var EventDispatcherinterface */
43        $dispatcher = $this->kernel->getContainer()->get('event_dispatcher');
44        $dispatcher->addListener(KernelEvents::TERMINATE, function () use ($cacheDir) {
45            @unlink($cacheDir . '/url_generating_routes.php');
46            @unlink($cacheDir . '/url_matching_routes.php');
47            @unlink($cacheDir . '/url_generating_routes.php.meta');
48            @unlink($cacheDir . '/url_matching_routes.php.meta');
49        });
50    }
51}