Passed
Pull Request — main (#140)
by Daniel
07:55 queued 02:24
created

FormCachePurgeCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 37
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A __construct() 0 5 1
A execute() 0 12 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\Command;
15
16
use Silverback\ApiComponentsBundle\Event\CommandLogEvent;
17
use Silverback\ApiComponentsBundle\Helper\Form\FormCachePurger;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Exception\InvalidArgumentException;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class FormCachePurgeCommand extends Command
28
{
29
    private FormCachePurger $formCachePurger;
30
    private EventDispatcherInterface $dispatcher;
31
32
    public function __construct(FormCachePurger $formCachePurger, EventDispatcherInterface $dispatcher, ?string $name = null)
33
    {
34
        $this->formCachePurger = $formCachePurger;
35
        $this->dispatcher = $dispatcher;
36
        parent::__construct($name);
37
    }
38
39
    /**
40
     * @throws InvalidArgumentException
41
     */
42
    protected function configure(): void
43
    {
44
        $this
45
            ->setName('silverback:api-components:form-cache-purge')
46
            ->setDescription('Purges the varnish cache for forms. Sets the `modified` timestamp to the file last modified date');
47
    }
48
49
    /**
50
     * @return int|void|null
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $this->dispatcher->addListener(
55
            CommandLogEvent::class,
56
            static function (CommandLogEvent $event) use ($output) {
57
                $output->writeln($event->getSubject());
58
            }
59
        );
60
        $this->formCachePurger->clear();
61
        $output->writeln('Form cache purge complete');
62
63
        return 0;
64
    }
65
}
66