Completed
Pull Request — master (#67)
by Matt
07:36 queued 15s
created

WanderUpdateFromGpxCommand::configure()   A

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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\ImageRepository;
6
use App\Repository\WanderRepository;
7
use App\Service\GpxService;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Helper\ProgressBar;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ConfirmationQuestion;
14
15
class WanderUpdateFromGpxCommand extends Command
16
{
17
    protected static $defaultName = 'wander:updatefromgpx';
18
19
    /** @var WanderRepository */
20
    private $wanderRepository;
21
22
    /** @var EntityManagerInterface */
23
    private $entityManager;
24
25
    /** @var GpxService */
26
    private $gpxService;
27
28
    public function __construct(WanderRepository $wanderRepository, EntityManagerInterface $entityManager, GpxService $gpxService)
29
    {
30
        $this->wanderRepository = $wanderRepository;
31
        $this->entityManager = $entityManager;
32
        $this->gpxService = $gpxService;
33
        parent::__construct();
34
    }
35
36
    protected function configure()
37
    {
38
        $this
39
            ->setDescription('Updates Wander data (including GeoJSON) with GPX information on all wanders.')
40
            ->setHelp('Updates GPX data on all wanders. Overwrites all existing data.');
41
    }
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $helper = $this->getHelper('question');
45
        $question = new ConfirmationQuestion('Are you sure you want to update all wanders based on their GPX track? ', false);
46
        if (!$helper->ask($input, $output, $question)) {
47
            $output->writeln('Aborting.');
48
            return Command::SUCCESS;
49
        }
50
51
        $wanders = $this->wanderRepository->findAll();
52
        $count = count($wanders);
53
        $output->writeln('Updating ' . $count . ' wanders');
54
55
        $progressBar = new ProgressBar($output, $count);
56
        $progressBar->start();
57
58
        foreach ($wanders as $wander) {
59
            $this->gpxService->updateWanderFromGpx($wander);
60
            $this->entityManager->persist($wander);
61
            $progressBar->advance();
62
        }
63
        $this->entityManager->flush();
64
        $progressBar->finish();
65
66
        return Command::SUCCESS;
67
68
    }
69
}