GenerateCacheCommand::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 1
    public function __construct(
52
        string $cacheDir,
53
        array $routesConfiguration,
54
        UrlProviderRegistry $registry,
55
        MessageCollectorInterface $messageCollector
56
    ) {
57 1
        $this->registry = $registry;
58 1
        $this->cacheDir = $cacheDir;
59 1
        $this->routesConfiguration = $routesConfiguration;
60
61 1
        parent::__construct();
62
63 1
        $this->messageCollector = $messageCollector;
64 1
    }
65
66 1
    protected function configure()
67
    {
68
        $this
69 1
            ->setDescription('Generate the urls cache used in the smoke tests. Urls are gathered by the urls providers')
70
        ;
71 1
    }
72
73 1
    protected function initialize(InputInterface $input, OutputInterface $output)
74
    {
75 1
        $this->fileSystem = new Filesystem();
76 1
        $this->messageCollector->initialize();
77 1
        $this->io = new SymfonyStyle($input, $output);
78 1
    }
79
80 1
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82 1
        $this->io->title('Smoker Generate Cache Command');
83
84
        // Create an empty cache file to initialize urls collecting
85 1
        $cacheFile = $this->createCacheFile();
86 1
        $urlCount = 0;
87
88 1
        foreach ($this->registry->all() as $provider) {
89
            $this->io->text('Processing the url provider "'.$provider->getName().'"...');
90
91
            $resolver = new OptionsResolver();
92
            $provider->configure($resolver);
93
            $urls = $provider->getCollection($resolver->resolve());
94
95
            $this->io->progressStart($urls->count());
96
97
            foreach ($urls->all() as $urlItem) {
98
                $providerCache = $urlItem->serialize().PHP_EOL;
99
                $this->fileSystem->appendToFile($cacheFile, $providerCache);
100
                $this->io->progressAdvance();
101
                ++$urlCount;
102
            }
103
            $this->io->progressFinish();
104
        }
105
106 1
        if ($urlCount > 0) {
107
            $this->io->success('The cache has been generated with '.$urlCount.' urls in '.$cacheFile);
108
        } else {
109 1
            $this->io->warning('No url was found in the configured url providers');
110
        }
111 1
    }
112
113 1
    protected function createCacheFile(): string
114
    {
115 1
        $this->io->text('Creating cache file...');
116 1
        $cacheFile = $this->cacheDir.'/smoker/smoker.cache';
117 1
        $this->fileSystem->dumpFile($cacheFile, '');
118
119 1
        return $cacheFile;
120
    }
121
}
122