Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

FormCachePurger::updateFormTimestamp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
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\Form\Cache;
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
    public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher)
33
    {
34
        $this->em = $em;
35
        $this->dispatcher = $dispatcher;
36
    }
37
38
    /**
39
     * @param string $cacheDir
40
     */
41
    public function clear($cacheDir = null): void
42
    {
43
        try {
44
            $repo = $this->em->getRepository(Form::class);
45
            /** @var Form[] $forms */
46
            $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
        if (!\count($forms)) {
53
            $this->dispatcher->dispatch(
54
                new CommandLogEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>')
55
            );
56
57
            return;
58
        }
59
        foreach ($forms as $form) {
60
            $this->updateFormTimestamp($form);
61
        }
62
        $this->em->flush();
63
    }
64
65
    private function updateFormTimestamp(Form $form): void
66
    {
67
        $formClass = $form->formType;
68
        $reflector = new ReflectionClass($formClass);
69
        $dateTime = new DateTime();
70
        $timestamp = filemtime($reflector->getFileName());
71
72
        $this->dispatcher->dispatch(new CommandLogEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)));
73
74
        if (!$form->modifiedAt || $timestamp !== $form->modifiedAt->getTimestamp()) {
75
            $dateTime->setTimestamp($timestamp);
76
            $form->modifiedAt = $dateTime;
77
            $this->dispatcher->dispatch(new CommandLogEvent('<comment>Updated timestamp</comment>'));
78
        }
79
    }
80
}
81