Completed
Push — master ( f7424d...b53e51 )
by Alejandro
07:43
created

GeneratePreviewCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 32
cts 34
cp 0.9412
rs 10
c 0
b 0
f 0
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 2
    public function __construct(
39
        ShortUrlServiceInterface $shortUrlService,
40
        PreviewGeneratorInterface $previewGenerator,
41
        TranslatorInterface $translator
42
    ) {
43 2
        $this->shortUrlService = $shortUrlService;
44 2
        $this->previewGenerator = $previewGenerator;
45 2
        $this->translator = $translator;
46 2
        parent::__construct(null);
47 2
    }
48
49 2
    public function configure()
50
    {
51 2
        $this->setName('shortcode:process-previews')
52 2
             ->setDescription(
53 2
                 $this->translator->translate(
54
                     'Processes and generates the previews for every URL, improving performance for later web requests.'
55 2
                 )
56 2
             );
57 2
    }
58
59 2
    public function execute(InputInterface $input, OutputInterface $output)
60
    {
61 2
        $page = 1;
62
        do {
63 2
            $shortUrls = $this->shortUrlService->listShortUrls($page);
64 2
            $page += 1;
65
66 2
            foreach ($shortUrls as $shortUrl) {
67 2
                $this->processUrl($shortUrl->getOriginalUrl(), $output);
68 2
            }
69 2
        } while ($page <= $shortUrls->count());
70
71 2
        $output->writeln('<info>' . $this->translator->translate('Finished processing all URLs') . '</info>');
72 2
    }
73
74 2
    protected function processUrl($url, OutputInterface $output)
75
    {
76
        try {
77 2
            $output->write(sprintf($this->translator->translate('Processing URL %s...'), $url));
78 2
            $this->previewGenerator->generatePreview($url);
79 1
            $output->writeln($this->translator->translate(' <info>Success!</info>'));
80 2
        } catch (PreviewGenerationException $e) {
81 1
            $messages = [' <error>' . $this->translator->translate('Error') . '</error>'];
82 1
            if ($output->isVerbose()) {
83
                $messages[] = '<error>' . $e->__toString() . '</error>';
84
            }
85
86 1
            $output->writeln($messages);
87
        }
88 2
    }
89
}
90