Completed
Push — v2 ( 418ab2...1227a6 )
by Daniel
05:04 queued 01:11
created

FormCachePurger::clear()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 20
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silverback\ApiComponentBundle\Form\Cache;
6
7
use DateTime;
8
use Doctrine\ORM\EntityManagerInterface;
9
use ReflectionClass;
10
use Silverback\ApiComponentBundle\ApiComponentBundleEvents;
11
use Silverback\ApiComponentBundle\Entity\Component\Form;
12
use Silverback\ApiComponentBundle\Event\CommandLogEvent;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
15
16
class FormCachePurger implements CacheClearerInterface
17
{
18
    private EntityManagerInterface $em;
19
    private EventDispatcherInterface $dispatcher;
20
21
    public function __construct(EntityManagerInterface $em, EventDispatcherInterface $dispatcher)
22
    {
23
        $this->em = $em;
24
        $this->dispatcher = $dispatcher;
25
    }
26
27
    /**
28
     * @param string $cacheDir
29
     */
30
    public function clear($cacheDir = null): void
31
    {
32
        try {
33
            $repo = $this->em->getRepository(Form::class);
34
            /** @var Form[] $forms */
35
            $forms = $repo->findAll();
36
        } catch (\Exception $exception) {
37
            $this->dispatcher->dispatch(
38
                new CommandLogEvent(sprintf('<error>Could not clear form cache: %s</error>', $exception->getMessage())),
39
                ApiComponentBundleEvents::COMMAND_LOG
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with Silverback\ApiComponentB...ndleEvents::COMMAND_LOG. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            $this->dispatcher->/** @scrutinizer ignore-call */ 
40
                               dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
40
            );
41
            return;
42
        }
43
        if (!\count($forms)) {
44
            $this->dispatcher->dispatch(
45
                new CommandLogEvent('<info>Skipping form component cache clear / timestamp updates - No forms components found</info>'),
46
                ApiComponentBundleEvents::COMMAND_LOG
47
            );
48
            return;
49
        }
50
        foreach ($forms as $form) {
51
            $this->updateFormTimestamp($form);
52
        }
53
        $this->em->flush();
54
    }
55
56
    private function updateFormTimestamp(Form $form): void
57
    {
58
        $formClass = $form->formType;
59
        $reflector = new ReflectionClass($formClass);
60
        $dateTime = new DateTime();
61
        $timestamp = filemtime($reflector->getFileName());
62
63
        $this->dispatcher->dispatch(
64
            new CommandLogEvent(sprintf('<info>Checking timestamp for %s</info>', $formClass)),
65
            ApiComponentBundleEvents::COMMAND_LOG
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with Silverback\ApiComponentB...ndleEvents::COMMAND_LOG. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $this->dispatcher->/** @scrutinizer ignore-call */ 
66
                           dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
        );
67
68
        if (!$form->modified || $timestamp !== $form->modified->getTimestamp()) {
69
            $dateTime->setTimestamp($timestamp);
70
            $form->modified = $dateTime;
71
            $this->dispatcher->dispatch(
72
                new CommandLogEvent('<comment>Updated timestamp</comment>'),
73
                ApiComponentBundleEvents::COMMAND_LOG
74
            );
75
        }
76
    }
77
}
78