Completed
Pull Request — master (#209)
by Alejandro
04:24
created

GeneratePreviewCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
5
6
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
7
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface;
8
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Zend\I18n\Translator\TranslatorInterface;
14
15
class GeneratePreviewCommand extends Command
16
{
17
    public const NAME = 'short-url:process-previews';
18
    private const ALIASES = ['shortcode:process-previews', 'short-code:process-previews'];
19
20
    /**
21
     * @var PreviewGeneratorInterface
22
     */
23
    private $previewGenerator;
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
    /**
29
     * @var ShortUrlServiceInterface
30
     */
31
    private $shortUrlService;
32
33 2
    public function __construct(
34
        ShortUrlServiceInterface $shortUrlService,
35
        PreviewGeneratorInterface $previewGenerator,
36
        TranslatorInterface $translator
37
    ) {
38 2
        $this->shortUrlService = $shortUrlService;
39 2
        $this->previewGenerator = $previewGenerator;
40 2
        $this->translator = $translator;
41 2
        parent::__construct(null);
42 2
    }
43
44 2
    protected function configure(): void
45
    {
46
        $this
47 2
            ->setName(self::NAME)
48 2
            ->setAliases(self::ALIASES)
49 2
            ->setDescription(
50 2
                $this->translator->translate(
51 2
                    'Processes and generates the previews for every URL, improving performance for later web requests.'
52
                )
53
            );
54 2
    }
55
56 2
    protected function execute(InputInterface $input, OutputInterface $output): void
57
    {
58 2
        $page = 1;
59
        do {
60 2
            $shortUrls = $this->shortUrlService->listShortUrls($page);
61 2
            $page += 1;
62
63 2
            foreach ($shortUrls as $shortUrl) {
64 2
                $this->processUrl($shortUrl->getOriginalUrl(), $output);
65
            }
66 2
        } while ($page <= $shortUrls->count());
67
68 2
        (new SymfonyStyle($input, $output))->success($this->translator->translate('Finished processing all URLs'));
69 2
    }
70
71 2
    private function processUrl($url, OutputInterface $output): void
72
    {
73
        try {
74 2
            $output->write(\sprintf($this->translator->translate('Processing URL %s...'), $url));
75 2
            $this->previewGenerator->generatePreview($url);
76 1
            $output->writeln($this->translator->translate(' <info>Success!</info>'));
77 1
        } catch (PreviewGenerationException $e) {
78 1
            $output->writeln(' <error>' . $this->translator->translate('Error') . '</error>');
79 1
            if ($output->isVerbose()) {
80
                $this->getApplication()->renderException($e, $output);
81
            }
82
        }
83 2
    }
84
}
85