Completed
Pull Request — master (#16)
by Arnaud
48:07 queued 29:02
created

GenerateCacheCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 3
b 0
f 0
nc 1
nop 4
dl 0
loc 13
rs 10
1
<?php
2
3
namespace LAG\SmokerBundle\Command;
4
5
use LAG\SmokerBundle\Message\MessageCollectorInterface;
6
use LAG\SmokerBundle\Url\Registry\UrlProviderRegistry;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
class GenerateCacheCommand extends Command
15
{
16
    protected static $defaultName = 'smoker:generate-cache';
17
18
    /**
19
     * @var UrlProviderRegistry
20
     */
21
    protected $registry;
22
23
    /**
24
     * @var string
25
     */
26
    protected $cacheDir;
27
28
    /**
29
     * @var array
30
     */
31
    protected $routesConfiguration;
32
33
    /**
34
     * @var Filesystem
35
     */
36
    protected $fileSystem;
37
38
    /**
39
     * @var MessageCollectorInterface
40
     */
41
    protected $messageCollector;
42
43
    /**
44
     * @var SymfonyStyle
45
     */
46
    protected $io;
47
48
    /**
49
     * GenerateCacheCommand constructor.
50
     *
51
     * @param string                    $cacheDir
52
     * @param array                     $routesConfiguration
53
     * @param UrlProviderRegistry       $registry
54
     * @param MessageCollectorInterface $messageCollector
55
     */
56
    public function __construct(
57
        string $cacheDir,
58
        array $routesConfiguration,
59
        UrlProviderRegistry $registry,
60
        MessageCollectorInterface $messageCollector
61
    ) {
62
        $this->registry = $registry;
63
        $this->cacheDir = $cacheDir;
64
        $this->routesConfiguration = $routesConfiguration;
65
66
        parent::__construct();
67
68
        $this->messageCollector = $messageCollector;
69
    }
70
71
    protected function configure()
72
    {
73
        $this
74
            ->setDescription('Generate the urls cache used in the smoke tests. Urls are gathered by the urls providers')
75
        ;
76
    }
77
78
    protected function initialize(InputInterface $input, OutputInterface $output)
79
    {
80
        $this->fileSystem = new Filesystem();
81
        $this->messageCollector->initialize();
82
        $this->io = new SymfonyStyle($input, $output);
83
    }
84
85
    protected function execute(InputInterface $input, OutputInterface $output)
86
    {
87
        $this->io->title('Smoker Generate Cache Command');
88
89
        // Create an empty cache file to initialize urls collecting
90
        $cacheFile = $this->createCacheFile();
91
        $urlCount = 0;
92
93
        foreach ($this->registry->all() as $provider) {
94
            $this->io->text('Processing the url provider "'.$provider->getName().'"...');
95
96
            $resolver = new OptionsResolver();
97
            $provider->configure($resolver);
98
            $urls = $provider->getCollection($resolver->resolve());
99
100
            $this->io->progressStart($urls->count());
101
102
            foreach ($urls->all() as $urlItem) {
103
                $providerCache = $urlItem->serialize().PHP_EOL;
104
                $this->fileSystem->appendToFile($cacheFile, $providerCache);
105
                $this->io->progressAdvance();
106
                ++$urlCount;
107
            }
108
            $this->io->progressFinish();
109
        }
110
111
        if ($urlCount > 0) {
112
            $this->io->success('The cache has been generated with '.$urlCount.' urls in '.$cacheFile);
113
        } else {
114
            $this->io->warning('No url was found in the configured url providers');
115
        }
116
    }
117
118
    protected function createCacheFile(): string
119
    {
120
        $this->io->text('Creating cache file...');
121
        $cacheFile = $this->cacheDir.'/smoker/smoker.cache';
122
        $this->fileSystem->dumpFile($cacheFile, '');
123
124
        return $cacheFile;
125
    }
126
}
127