WanderSetImageLocationsCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 30 4
A __construct() 0 11 1
A configure() 0 5 1
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\WanderRepository;
6
use App\Service\ImageService;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
class WanderSetImageLocationsCommand extends Command
17
{
18
    protected static $defaultName = 'wander:set-image-locations';
19
    protected static $defaultDescription = 'Updates Location field from Exif information for all images for a given wander';
20
21
    /** @var WanderRepository */
22
    private $wanderRepository;
23
24
    /** @var ImageService */
25
    private $imageService;
26
27
    /** @var EntityManagerInterface */
28
    private $entityManager;
29
30
    public function __construct(
31
        WanderRepository $wanderRepository,
32
        ImageService $imageService,
33
        EntityManagerInterface $entityManager
34
    )
35
    {
36
        $this->wanderRepository = $wanderRepository;
37
        $this->imageService = $imageService;
38
        $this->entityManager = $entityManager;
39
40
        parent::__construct();
41
    }
42
43
    protected function configure()
44
    {
45
        $this
46
            ->setDescription(self::$defaultDescription)
47
            ->addArgument('id', InputArgument::REQUIRED, 'Wander ID')
48
        ;
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $io = new SymfonyStyle($input, $output);
54
        $id = filter_var($input->getArgument('id'), FILTER_VALIDATE_INT, ['min_range' => 0]);
55
        if ($id === false) {
56
            $output->writeln('id must be an integer.');
57
            return Command::FAILURE;
58
        }
59
60
        $wander = $this->wanderRepository->find($id);
61
        if ($wander === null) {
62
            $io->error("Failed to find wander $id");
63
            return Command::FAILURE;
64
        }
65
66
        $images = $wander->getImages();
67
        $progressBar = new ProgressBar($output, count($images));
68
        $progressBar->start();
69
70
        foreach ($images as $image) {
71
            $this->imageService->setLocationFromEXIF($image);
72
            $this->entityManager->persist($image);
73
            $this->entityManager->flush();
74
            $progressBar->advance();
75
        }
76
        $progressBar->finish();
77
78
        $io->success('Image locations updated.');
79
80
        return Command::SUCCESS;
81
    }
82
}
83