HttpCacheWarmupCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 4

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 52
ccs 32
cts 32
cp 1
rs 8.9408
cc 4
eloc 29
nc 3
nop 2
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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