Completed
Push — master ( 437eb2...c80e02 )
by Matt
04:57
created

ImagesTagCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 48
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 5 1
A execute() 0 30 4
A tagImage() 0 25 3
1
<?php
2
3
namespace App\Command;
4
5
use App\Entity\Image;
6
use App\Repository\WanderRepository;
7
use Doctrine\ORM\EntityManagerInterface;
8
use ErrorException;
9
use Exception;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
use Symfony\Contracts\HttpClient\HttpClientInterface;
18
19
class ImagesTagCommand extends Command
20
{
21
    protected static $defaultName = 'images:tag';
22
    /** @var HttpClientInterface */
23
    private $imagga;
24
25
    /** @var WanderRepository */
26
    private $wanderRepository;
27
28
    /** @var EntityManagerInterface */
29
    private $entityManager;
30
31
    public function __construct(HttpClientInterface $imagga, WanderRepository $wanderRepository, EntityManagerInterface $entityManager)
32
    {
33
        $this->imagga = $imagga;
34
        $this->wanderRepository = $wanderRepository;
35
        $this->entityManager = $entityManager;
36
        parent::__construct();
37
    }
38
39
    protected function configure(): void
40
    {
41
        $this
42
            ->setDescription('Retrieve and apply imagga tags to all images in a Wander')
43
            ->addArgument('id', InputArgument::REQUIRED, 'Wander ID')
44
            // TODO: Add option to overwrite existing tags
45
            //->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
46
        ;
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output): int
50
    {
51
        $helper = $this->getHelper('question');
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
52
        $io = new SymfonyStyle($input, $output);
53
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
        foreach ($images as $image) {
70
            $this->tagImage($image, $input, $output);
71
            $progressBar->advance();
72
        }
73
        $this->entityManager->flush();
74
        $progressBar->finish();
75
76
        $io->success("Tagged the wander's images.");
77
78
        return Command::SUCCESS;
79
    }
80
81
    protected function tagImage(Image $image, InputInterface $input, OutputInterface $output): void
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed. ( Ignorable by Annotation )

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

81
    protected function tagImage(Image $image, /** @scrutinizer ignore-unused */ InputInterface $input, OutputInterface $output): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        $response = $this->imagga->request('GET', 'https://api.imagga.com/v2/tags', [
84
            'query' => [
85
                'image_url' => $image->getMediumImageUri(),
86
                'threshold' => 15.0
87
            ]
88
        ]);
89
90
        $content = $response->getContent(false);
91
        $imagga_result = json_decode($content);
92
93
        if ($imagga_result->status->type != 'success') {
94
            $output->writeln('<error>Error returned from imagga:</error>');
95
            $output->writeln('<error> Type: ' . $imagga_result->status->type . '</error>');
96
            $output->writeln('<error> Text: ' . $imagga_result->status->text . '</error>');
97
            throw new ErrorException("Error returned from imagga");
98
        }
99
100
        $tags = [];
101
        foreach($imagga_result->result->tags as $tag) {
102
            $tags[] = $tag->tag->en;
103
        }
104
        $image->setAutoTags($tags);
105
        $this->entityManager->persist($image);
106
    }
107
}
108