DeleteAllWandersCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\WanderRepository;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ConfirmationQuestion;
12
13
class DeleteAllWandersCommand extends Command
14
{
15
    protected static $defaultName = 'wanders:delete';
16
17
    /** @var WanderRepository */
18
    private $wanderRepository;
19
20
    /** @var EntityManagerInterface */
21
    private $entityManager;
22
23
    public function __construct(WanderRepository $wanderRepository, EntityManagerInterface $entityManager)
24
    {
25
        $this->wanderRepository = $wanderRepository;
26
        $this->entityManager = $entityManager;
27
        parent::__construct();
28
    }
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setDescription('Deletes all Wanders.')
34
            ->setHelp('Deletes all Wander entities and their associated uploaded files.');
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39
        $helper = $this->getHelper('question');
40
        $question = new ConfirmationQuestion('Are you sure you want to delete ALL wanders? ', false);
41
        if (!$helper->ask($input, $output, $question)) {
42
            $output->writeln('Aborting.');
43
            return Command::SUCCESS;
44
        }
45
46
        $wanders = $this->wanderRepository->findAll();
47
        $count = count($wanders);
48
        $output->writeln('Deleting ' . $count . ' images');
49
50
        $progressBar = new ProgressBar($output, $count);
51
        $progressBar->start();
52
53
        foreach ($wanders as $wander) {
54
            $this->entityManager->remove($wander);
55
            $progressBar->advance();
56
        }
57
        $this->entityManager->flush();
58
        $progressBar->finish();
59
60
        return Command::SUCCESS;
61
62
    }
63
}