CacheFlushCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Matecat\SimpleS3\Console;
4
5
use Matecat\SimpleS3\Client;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class CacheFlushCommand extends Command
11
{
12
    /**
13
     * @var Client
14
     */
15
    private Client $s3Client;
16
17
    /**
18
     * CacheFlushCommand constructor.
19
     *
20
     * @param Client      $s3Client
21
     * @param string|null $name
22
     */
23
    public function __construct(Client $s3Client, ?string $name = null)
24
    {
25
        parent::__construct($name);
26
27
        $this->s3Client = $s3Client;
28
    }
29
30
    protected function configure(): void
31
    {
32
        $this
33
                ->setName('ss3:cache:flush')
34
                ->setDescription('Flush all data stored in cache.')
35
                ->setHelp('This command flushes all data stored in cache.');
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output): int
39
    {
40
        if (false === $this->s3Client->hasCache()) {
41
            throw new \Exception('Cache in not enabled. You have to enable caching to use this command');
42
        }
43
44
        if (true === $this->s3Client->getCache()->flushAll()) {
45
            $output->writeln('<fg=green>Cache was successful flushed.</>');
46
47
            return 0;
48
        } else {
49
            $output->writeln('<fg=red>Error during cache flushing.</>');
50
51
            return 1;
52
        }
53
    }
54
}
55