Completed
Push — master ( 065cdd...96faaf )
by Alejandro
09:28
created

GeneratePreviewCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 75
rs 10
wmc 8
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A configure() 0 9 1
A execute() 0 14 3
A processUrl() 0 15 3
1
<?php
2
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
6
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
7
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface;
8
use Shlinkio\Shlink\Core\Service\ShortUrlService;
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 Zend\I18n\Translator\TranslatorInterface;
14
15
class GeneratePreviewCommand extends Command
16
{
17
    /**
18
     * @var PreviewGeneratorInterface
19
     */
20
    private $previewGenerator;
21
    /**
22
     * @var TranslatorInterface
23
     */
24
    private $translator;
25
    /**
26
     * @var ShortUrlServiceInterface
27
     */
28
    private $shortUrlService;
29
30
    /**
31
     * GeneratePreviewCommand constructor.
32
     * @param ShortUrlServiceInterface $shortUrlService
33
     * @param PreviewGeneratorInterface $previewGenerator
34
     * @param TranslatorInterface $translator
35
     *
36
     * @Inject({ShortUrlService::class, PreviewGenerator::class, "translator"})
37
     */
38
    public function __construct(
39
        ShortUrlServiceInterface $shortUrlService,
40
        PreviewGeneratorInterface $previewGenerator,
41
        TranslatorInterface $translator
42
    ) {
43
        $this->shortUrlService = $shortUrlService;
44
        $this->previewGenerator = $previewGenerator;
45
        $this->translator = $translator;
46
        parent::__construct(null);
47
    }
48
49
    public function configure()
50
    {
51
        $this->setName('shortcode:process-previews')
52
             ->setDescription(
53
                 $this->translator->translate(
54
                     'Processes and generates the previews for every URL, improving performance for later web requests.'
55
                 )
56
             );
57
    }
58
59
    public function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $page = 1;
62
        do {
63
            $shortUrls = $this->shortUrlService->listShortUrls($page);
64
            $page += 1;
65
66
            foreach ($shortUrls as $shortUrl) {
67
                $this->processUrl($shortUrl->getOriginalUrl(), $output);
68
            }
69
        } while ($page <= $shortUrls->count());
70
71
        $output->writeln('<info>' . $this->translator->translate('Finished processing all URLs') . '</info>');
72
    }
73
74
    protected function processUrl($url, OutputInterface $output)
75
    {
76
        try {
77
            $output->write(sprintf($this->translator->translate('Processing URL %s...'), $url));
78
            $this->previewGenerator->generatePreview($url);
79
            $output->writeln($this->translator->translate(' <info>Success!</info>'));
80
        } catch (PreviewGenerationException $e) {
81
            $messages = [' <error>' . $this->translator->translate('Error') . '</error>'];
82
            if ($output->isVerbose()) {
83
                $messages[] = '<error>' . $e->__toString() . '</error>';
84
            }
85
86
            $output->writeln($messages);
87
        }
88
    }
89
}
90