HttpCacheWarmupCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 5
c 6
b 2
f 0
lcom 0
cbo 10
dl 0
loc 68
ccs 37
cts 37
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 0 52 4
1
<?php
2
3
namespace Zenstruck\CacheBundle\Command;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Helper\ProgressBar;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\HttpFoundation\Response;
13
use Zenstruck\CacheBundle\Url\Crawler;
14
15
/**
16
 * @author Kevin Bond <[email protected]>
17
 */
18
class HttpCacheWarmupCommand extends ContainerAwareCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 4
    protected function configure()
24
    {
25 4
        $this
26 4
            ->setName('zenstruck:http-cache:warmup')
27 2
            ->setDescription('Warms up an http cache');
28 4
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 4
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        /** @var Crawler $crawler */
36 2
        $crawler = $this->getContainer()->get('zenstruck_cache.crawler');
37 4
        $summary = [];
38 2
        $total = count($crawler);
39 2
        $progress = new ProgressBar($output, $total);
40
41 4
        if (0 === $total) {
42 1
            throw new \RuntimeException('No URL providers registered.');
43
        }
44
45 1
        $output->writeln("\n<comment>Beginning http cache warmup.</comment>");
46 1
        $progress->start();
47
48 2
        $callback = function (ResponseInterface $response) use (&$summary, $progress) {
49 1
            $status = $response->getStatusCode();
50
51 1
            $progress->advance();
52
53 1
            if (!array_key_exists($status, $summary)) {
54 1
                $summary[$status] = 1;
55
56 1
                return;
57
            }
58
59 2
            ++$summary[$status];
60 1
        };
61
62 1
        $crawler->crawl($callback);
63
64 1
        $progress->finish();
65 1
        $output->writeln("\n");
66
67 1
        ksort($summary);
68
69 1
        $output->writeln('<comment>Summary:</comment>');
70
71 1
        $table = new Table($output);
72
73 1
        $table->setHeaders(['Code', 'Reason', 'Count']);
74
75 1
        foreach ($summary as $code => $count) {
76 1
            $table->addRow([$code, Response::$statusTexts[$code], $count]);
77 1
        }
78
79 1
        $table->addRow(new TableSeparator());
80 1
        $table->addRow(['', 'Total', $total]);
81
82 1
        $table->render();
83 1
        $output->writeln('');
84 2
    }
85
}
86