Passed
Push — v2 ( c4de3c...f057b3 )
by Brice
03:43
created

FlushTasks   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 41.18%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 37
ccs 7
cts 17
cp 0.4118
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 18 3
1
<?php
2
3
namespace JobQueue\Application\Console;
4
5
use JobQueue\Application\Utils\CommandTrait;
6
use JobQueue\Infrastructure\ServiceContainer;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ConfirmationQuestion;
12
13
final class FlushTasks extends Command
14
{
15
    use CommandTrait;
16
17
    public function configure()
18
    {
19
        $this
20
            ->setName('flush')
21
            ->setDescription('Deletes all tasks from the queue')
22
            ->addOption('force', 'y', InputOption::VALUE_NONE, 'Skip validation')
23
        ;
24
    }
25
26
    /**
27
     *
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     * @return int
31
     */
32 1
    protected function execute(InputInterface $input, OutputInterface $output): int
33
    {
34 1
        if (!$input->getOption('force')) {
35
            $helper = $this->getHelper('question'); /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
36
            $question = new ConfirmationQuestion('Do you want to delete all tasks from the queue? (y/f) ', false);
37
            if (!$helper->ask($input, $output, $question)) {
38
                $output->writeln('Canceled');
39
                return 0;
40
            }
41
        }
42
43 1
        ServiceContainer::getInstance()
44 1
            ->queue
45 1
            ->flush();
46
47 1
        $this->formatInfoSection('Queue flushed', $output);
48
49 1
        return 0;
50
    }
51
}
52