Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

GeneratePreviewCommand::processUrl()   A

Complexity

Conditions 3
Paths 7

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

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