Passed
Pull Request — master (#357)
by Alejandro
05:24
created

GeneratePreviewCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
wmc 8
eloc 29
dl 0
loc 53
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 14 3
A configure() 0 7 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\CLI\Util\ExitCodes;
7
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
8
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface;
9
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
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
    /** @var PreviewGeneratorInterface */
22
    private $previewGenerator;
23
    /** @var ShortUrlServiceInterface */
24
    private $shortUrlService;
25
26 2
    public function __construct(ShortUrlServiceInterface $shortUrlService, PreviewGeneratorInterface $previewGenerator)
27
    {
28 2
        parent::__construct();
29 2
        $this->shortUrlService = $shortUrlService;
30 2
        $this->previewGenerator = $previewGenerator;
31
    }
32
33 2
    protected function configure(): void
34
    {
35
        $this
36 2
            ->setName(self::NAME)
37 2
            ->setAliases(self::ALIASES)
38 2
            ->setDescription(
39 2
                'Processes and generates the previews for every URL, improving performance for later web requests.'
40
            );
41
    }
42
43 2
    protected function execute(InputInterface $input, OutputInterface $output): ?int
44
    {
45 2
        $page = 1;
46
        do {
47 2
            $shortUrls = $this->shortUrlService->listShortUrls($page);
48 2
            $page += 1;
49
50 2
            foreach ($shortUrls as $shortUrl) {
51 2
                $this->processUrl($shortUrl->getLongUrl(), $output);
52
            }
53 2
        } while ($page <= $shortUrls->count());
54
55 2
        (new SymfonyStyle($input, $output))->success('Finished processing all URLs');
56 2
        return ExitCodes::EXIT_SUCCESS;
57
    }
58
59 2
    private function processUrl($url, OutputInterface $output): void
60
    {
61
        try {
62 2
            $output->write(sprintf('Processing URL %s...', $url));
63 2
            $this->previewGenerator->generatePreview($url);
64 1
            $output->writeln(' <info>Success!</info>');
65 1
        } catch (PreviewGenerationException $e) {
66 1
            $output->writeln(' <error>Error</error>');
67 1
            if ($output->isVerbose()) {
68
                $this->getApplication()->renderException($e, $output);
69
            }
70
        }
71
    }
72
}
73