UpdateImagesFromExifCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 6
nop 2
dl 0
loc 34
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\ImageRepository;
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\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ConfirmationQuestion;
14
15
class UpdateImagesFromExifCommand extends Command
16
{
17
    protected static $defaultName = 'images:updatefromexif';
18
19
    /** @var ImageRepository */
20
    private $imageRepository;
21
22
    /** @var EntityManagerInterface */
23
    private $entityManager;
24
25
    /** @var ImageService */
26
    private $imageService;
27
28
    public function __construct(ImageRepository $imageRepository, EntityManagerInterface $entityManager, ImageService $imageService)
29
    {
30
        $this->imageRepository = $imageRepository;
31
        $this->entityManager = $entityManager;
32
        $this->imageService = $imageService;
33
        parent::__construct();
34
    }
35
36
    protected function configure()
37
    {
38
        $this
39
            ->setDescription('Updates all images based on their EXIF information.')
40
            ->setHelp('Updates image properties based on their EXIF information, for all images. Overwrites existing data, except for related wanders.')
41
            ->addOption('update-wanders', null, InputOption::VALUE_NONE, 'Find related wanders by matching times, and add relationships.');
42
    }
43
    protected function execute(InputInterface $input, OutputInterface $output): int
44
    {
45
        $updateWanders = $input->getOption('update-wanders');
46
47
        $helper = $this->getHelper('question');
48
49
        if ($updateWanders) {
50
            $questionText = 'Are you sure you want to update properties (*including* related Wanders) for all images based on their EXIF data? ';
51
        } else {
52
            $questionText = 'Are you sure you want to update properties (except related Wanders) for all images based on their EXIF data? ';
53
        }
54
55
        $question = new ConfirmationQuestion($questionText, false);
56
        if (!$helper->ask($input, $output, $question)) {
57
            $output->writeln('Aborting.');
58
            return Command::SUCCESS;
59
        }
60
61
        $images = $this->imageRepository->findAll();
62
        $count = count($images);
63
        $output->writeln('Updating ' . $count . ' images');
64
65
        $progressBar = new ProgressBar($output, $count);
66
        $progressBar->start();
67
68
        foreach ($images as $image) {
69
            $this->imageService->setPropertiesFromEXIF($image, $updateWanders);
70
            $this->entityManager->persist($image);
71
            $progressBar->advance();
72
        }
73
        $this->entityManager->flush();
74
        $progressBar->finish();
75
        $output->writeln("\nImages updated.");
76
        return Command::SUCCESS;
77
    }
78
}