Issues (18)

src/Command/ImagesUpdatelocationsCommand.php (1 issue)

Labels
Severity
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->findWithNoLocationButHasLatLng();
53
        $total = count($images);
0 ignored issues
show
It seems like $images can also be of type integer; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $total = count(/** @scrutinizer ignore-type */ $images);
Loading history...
54
        $success = 0;
55
        $failure = 0;
56
57
        $progressBar = new ProgressBar($output, $total);
58
        $progressBar->start();
59
        foreach ($images as $image) {
60
            $neighbourhood = $this->locationService->getLocationName($image->getLatitude(), $image->getLongitude());
61
            if ($neighbourhood !== null) {
62
                $image->setLocation($neighbourhood);
63
                $this->entityManager->persist($image);
64
                $this->entityManager->flush(); // It actually seems faster to flush in the loop, rather than afterwards. Odd.
65
                $success++;
66
            } else {
67
                $failure++;
68
            }
69
            $progressBar->advance();
70
        }
71
        $progressBar->finish();
72
73
        $io->success("Tried to locate $total images. Success: $success. Failure: $failure.");
74
75
        return Command::SUCCESS;
76
    }
77
}
78