Completed
Push — master ( a0d1e1...4fda4b )
by Matt
04:38
created

ImagesUpdatelocationsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Command;
4
5
use App\Repository\ImageRepository;
6
use App\Service\LocationService;
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\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class ImagesUpdatelocationsCommand extends Command
15
{
16
    protected static $defaultName = 'images:updatelocations';
17
    protected static $defaultDescription = "Updates images whose locations aren't set using our location service.";
18
19
    /** @var ImageRepository */
20
    private $imageRepository;
21
22
    /** @var LocationService */
23
    private $locationService;
24
25
    /** @var EntityManagerInterface */
26
    private $entityManager;
27
28
    public function __construct(
29
        ImageRepository $imageRepository,
30
        LocationService $locationService,
31
        EntityManagerInterface $entityManager
32
    )
33
    {
34
        $this->imageRepository = $imageRepository;
35
        $this->locationService = $locationService;
36
        $this->entityManager = $entityManager;
37
38
        parent::__construct();
39
    }
40
41
    protected function configure(): void
42
    {
43
        $this->setDescription(self::$defaultDescription);
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output): int
47
    {
48
        $io = new SymfonyStyle($input, $output);
49
50
        $io->info("Attempting to add locations to images missing locations.");
51
52
        $images = $this->imageRepository->findWithNoLocation();
53
        $total = count($images);
54
        $success = 0;
55
        $failure = 0;
56
57
        $progressBar = new ProgressBar($output, $total);
58
        $progressBar->start();
59
        foreach ($images as $image) {
60
            $result = $this->locationService->setImageLocation($image, true);
61
            if ($result) {
62
                $success++;
63
            } else {
64
                $failure++;
65
            }
66
            $this->entityManager->persist($image);
67
            $this->entityManager->flush(); // It actually seems faster to flush in the loop, rather than afterwards. Odd.
68
            $progressBar->advance();
69
        }
70
        $progressBar->finish();
71
72
        $io->success("Tried to locate $total images. Success: $success. Failure: $failure.");
73
74
        return Command::SUCCESS;
75
    }
76
}
77