Passed
Pull Request — master (#66)
by Daniel
05:32
created

FormCachePurger::clear()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 5
nop 1
dl 0
loc 20
ccs 0
cts 12
cp 0
crap 20
rs 9.8666
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\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
    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(new CommandLogEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>'));
54
55
            return;
56
        }
57
        foreach ($forms as $form) {
58
            $this->updateFormTimestamp($form);
59
        }
60
        $this->em->flush();
61
    }
62
63
    private function updateFormTimestamp(Form $form): void
64
    {
65
        $formClass = $form->formType;
66
        $reflector = new ReflectionClass($formClass);
67
        $dateTime = new DateTime();
68
        $timestamp = filemtime($reflector->getFileName());
69
70
        $this->dispatcher->dispatch(new CommandLogEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)));
71
72
        if (!$form->modifiedAt || $timestamp !== $form->modifiedAt->getTimestamp()) {
73
            $dateTime->setTimestamp($timestamp);
74
            $form->modifiedAt = $dateTime;
75
            $this->dispatcher->dispatch(new CommandLogEvent('<comment>Updated timestamp</comment>'));
76
        }
77
    }
78
}
79