Passed
Push — master ( 90e201...b182a5 )
by Matt
05:14
created

ImagesTagCommand::tagImage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 26
rs 9.7333
1
<?php
2
3
namespace App\Command;
4
5
use App\Entity\Image;
6
use App\Repository\WanderRepository;
7
use App\Service\ImaggaService;
8
use Doctrine\ORM\EntityManagerInterface;
9
use ErrorException;
10
use Exception;
11
use GuzzleHttp\Client;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Helper\ProgressBar;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Style\SymfonyStyle;
19
use Symfony\Component\Routing\RouterInterface;
20
use Symfony\Contracts\HttpClient\HttpClientInterface;
21
22
class ImagesTagCommand extends Command
23
{
24
    protected static $defaultName = 'images:tag';
25
    /** @var ImaggaService */
26
    private $imaggaService;
27
28
    /** @var WanderRepository */
29
    private $wanderRepository;
30
31
    /** @var EntityManagerInterface */
32
    private $entityManager;
33
34
    /** @var RouterInterface */
35
    private $router;
36
37
    public function __construct(
38
        //Client $imaggaClient,
39
        ImaggaService $imaggaService,
40
        WanderRepository $wanderRepository,
41
        EntityManagerInterface $entityManager,
42
        RouterInterface $router
43
        )
44
    {
45
        $this->imaggaService = $imaggaService;
46
        $this->wanderRepository = $wanderRepository;
47
        $this->entityManager = $entityManager;
48
        $this->router = $router;
49
50
        parent::__construct();
51
    }
52
53
    protected function configure(): void
54
    {
55
        $this
56
            ->setDescription('Retrieve and apply imagga tags to all images in a Wander')
57
            ->addArgument('id', InputArgument::REQUIRED, 'Wander ID')
58
            // TODO: Add option to overwrite existing tags
59
            //->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
60
        ;
61
    }
62
63
    protected function execute(InputInterface $input, OutputInterface $output): int
64
    {
65
        $helper = $this->getHelper('question');
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
66
        $io = new SymfonyStyle($input, $output);
67
68
        $id = filter_var($input->getArgument('id'), FILTER_VALIDATE_INT, ['min_range' => 0]);
69
        if ($id === false) {
70
            $output->writeln('id must be an integer.');
71
            return Command::FAILURE;
72
        }
73
74
        $wander = $this->wanderRepository->find($id);
75
        if ($wander == null) {
76
            $io->error("Failed to find wander $id");
77
            return Command::FAILURE;
78
        }
79
80
        // TODO: As we're a Command, we don't have a Request to set these up.
81
        // Bodge it manually for now. You can configure these in config/services.yml;
82
        // see https://symfony.com/doc/4.1/console/request_context.html and
83
        // do that later.
84
85
        $context = $this->router->getContext();
86
        $context->setHost('omm.gothick.org.uk');
87
        $context->setScheme('https');
88
89
        $images = $wander->getImages();
90
        $progressBar = new ProgressBar($output, count($images));
91
        $progressBar->start();
92
        foreach ($images as $image) {
93
            $this->imaggaService->tagImage($image);
94
            $progressBar->advance();
95
        }
96
        $progressBar->finish();
97
98
        $io->success("Tagged the wander's images.");
99
100
        return Command::SUCCESS;
101
    }
102
}
103