Completed
Pull Request — master (#246)
by Alejandro
05:59
created

GeneratePreviewCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 66
rs 10
c 0
b 0
f 0
ccs 29
cts 30
cp 0.9667
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A execute() 0 13 3
A configure() 0 8 1
A processUrl() 0 10 3
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
use function sprintf;
15
16
class GeneratePreviewCommand extends Command
17
{
18
    public const NAME = 'short-url:process-previews';
19
    private const ALIASES = ['shortcode:process-previews', 'short-code:process-previews'];
20
21
    /**
22
     * @var PreviewGeneratorInterface
23
     */
24
    private $previewGenerator;
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
    /**
30
     * @var ShortUrlServiceInterface
31
     */
32
    private $shortUrlService;
33
34 2
    public function __construct(
35
        ShortUrlServiceInterface $shortUrlService,
36
        PreviewGeneratorInterface $previewGenerator,
37
        TranslatorInterface $translator
38
    ) {
39 2
        $this->shortUrlService = $shortUrlService;
40 2
        $this->previewGenerator = $previewGenerator;
41 2
        $this->translator = $translator;
42 2
        parent::__construct(null);
43 2
    }
44
45 2
    protected function configure(): void
46
    {
47
        $this
48 2
            ->setName(self::NAME)
49 2
            ->setAliases(self::ALIASES)
50 2
            ->setDescription(
51 2
                $this->translator->translate(
52 2
                    'Processes and generates the previews for every URL, improving performance for later web requests.'
53
                )
54
            );
55 2
    }
56
57 2
    protected function execute(InputInterface $input, OutputInterface $output): void
58
    {
59 2
        $page = 1;
60
        do {
61 2
            $shortUrls = $this->shortUrlService->listShortUrls($page);
62 2
            $page += 1;
63
64 2
            foreach ($shortUrls as $shortUrl) {
65 2
                $this->processUrl($shortUrl->getOriginalUrl(), $output);
0 ignored issues
show
Deprecated Code introduced by
The function Shlinkio\Shlink\Core\Ent...rtUrl::getOriginalUrl() has been deprecated: Use getLongUrl() instead ( Ignorable by Annotation )

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

65
                $this->processUrl(/** @scrutinizer ignore-deprecated */ $shortUrl->getOriginalUrl(), $output);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66
            }
67 2
        } while ($page <= $shortUrls->count());
68
69 2
        (new SymfonyStyle($input, $output))->success($this->translator->translate('Finished processing all URLs'));
70 2
    }
71
72 2
    private function processUrl($url, OutputInterface $output): void
73
    {
74
        try {
75 2
            $output->write(sprintf($this->translator->translate('Processing URL %s...'), $url));
76 2
            $this->previewGenerator->generatePreview($url);
77 1
            $output->writeln($this->translator->translate(' <info>Success!</info>'));
78 1
        } catch (PreviewGenerationException $e) {
79 1
            $output->writeln(' <error>' . $this->translator->translate('Error') . '</error>');
80 1
            if ($output->isVerbose()) {
81
                $this->getApplication()->renderException($e, $output);
82
            }
83
        }
84 2
    }
85
}
86