Passed
Push — master ( def3b9...9746d2 )
by Daniel
06:04
created

FormCachePurger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 49
ccs 24
cts 27
cp 0.8889
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateFormTimestamp() 0 13 3
A clear() 0 20 4
A __construct() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Helper\Form;
15
16
use DateTime;
17
use Doctrine\ORM\EntityManagerInterface;
18
use ReflectionClass;
19
use Silverback\ApiComponentsBundle\Entity\Component\Form;
20
use Silverback\ApiComponentsBundle\Event\CommandLogEvent;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class FormCachePurger implements CacheClearerInterface
28
{
29
    private EntityManagerInterface $em;
30
    private EventDispatcherInterface $dispatcher;
31
32 7
    public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher)
33
    {
34 7
        $this->em = $em;
35 7
        $this->dispatcher = $dispatcher;
36 7
    }
37
38
    /**
39
     * @param string $cacheDir
40
     */
41 2
    public function clear($cacheDir = null): void
42
    {
43
        try {
44 2
            $repo = $this->em->getRepository(Form::class);
45
            /** @var Form[] $forms */
46 2
            $forms = $repo->findAll();
47
        } catch (\Exception $exception) {
48
            $this->dispatcher->dispatch(new CommandLogEvent(sprintf('<error>Could not clear form cache: %s</error>', $exception->getMessage())));
49
50
            return;
51
        }
52 2
        if (!\count($forms)) {
53 1
            $this->dispatcher->dispatch(new CommandLogEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>'));
54
55 1
            return;
56
        }
57 1
        foreach ($forms as $form) {
58 1
            $this->updateFormTimestamp($form);
59
        }
60 1
        $this->em->flush();
61 1
    }
62
63 1
    private function updateFormTimestamp(Form $form): void
64
    {
65 1
        $formClass = $form->formType;
66 1
        $reflector = new ReflectionClass($formClass);
67 1
        $dateTime = new DateTime();
68 1
        $timestamp = filemtime($reflector->getFileName());
69
70 1
        $this->dispatcher->dispatch(new CommandLogEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)));
71
72 1
        if (!$form->modifiedAt || $timestamp !== $form->modifiedAt->getTimestamp()) {
73 1
            $dateTime->setTimestamp($timestamp);
74 1
            $form->modifiedAt = $dateTime;
75 1
            $this->dispatcher->dispatch(new CommandLogEvent('<comment>Updated timestamp</comment>'));
76
        }
77 1
    }
78
}
79