Completed
Pull Request — 2.x (#1258)
by
unknown
02:22
created

WarmCacheCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Command;
13
14
use Liip\ImagineBundle\Imagine\Cache\CacheWarmer;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * A command to warm up images cache (i.e. pre-generate thumbnails)
23
 *
24
 * @author Konstantin Tjuterev <[email protected]>
25
 */
26
class WarmCacheCommand extends ContainerAwareCommand
27
{
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('liip:imagine:cache:warm')
32
            ->setDescription('Warms cache for paths provided by given warmers (or all warmers, if run w/o params)')
33
            ->addOption('chunk-size', 'c', InputOption::VALUE_REQUIRED, 'Chunk size', 100)
34
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force cache warm up for already cached images')
35
            ->addArgument('warmers', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Warmers names')
36
            ->setHelp(<<<'EOF'
37
The <info>%command.name%</info> command warms up cache by specified parameters.
38
39
A warmer can be configured for one or more filter set. A warmer should return a list of paths.
40
This command gets the paths from warmer and create cache (i.e. filtered image) for each filter configured for given warmer.
41
42
Warmers should be separated by spaces:
43
<info>php app/console %command.name% warmer1 warmer2</info>
44
All cache for a given `warmers` will be warmed up
45
46
<info>php app/console %command.name%</info>
47
Cache for all warmers will be warmed up when executing this command without parameters.
48
49
Note, that <info>--force</info> option will force regeneration of the cache only if warmer returns the path.
50
Generally, there should be NO need to use this option, instead, use <info>liip:imagine:cache:remove</info> command to clear cache.
51
Then run this command to warm-up the cache
52
EOF
53
            );
54
    }
55
56
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58
        $warmers = $input->getArgument('warmers');
59
60
        /** @var CacheWarmer $cacheWarmer */
61
        $cacheWarmer = $this->getContainer()->get('liip_imagine.cache.warmer');
62
        $cacheWarmer->setLoggerClosure($this->getLoggerClosure($output));
63
64
        if ($chunkSize = $input->getOption('chunk-size')) {
65
            $cacheWarmer->setChunkSize($chunkSize);
66
        }
67
68
        $force = false;
69
        if ($input->getOption('force')) {
70
            $force = true;
71
        }
72
73
        $cacheWarmer->warm($force, $warmers);
0 ignored issues
show
Bug introduced by
It seems like $warmers defined by $input->getArgument('warmers') on line 58 can also be of type string; however, Liip\ImagineBundle\Imagi...che\CacheWarmer::warm() does only seem to accept array|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
    }
75
76
    /**
77
     * Returns Logger Closure
78
     *
79
     * @return callable
80
     */
81
    protected function getLoggerClosure(OutputInterface $output)
82
    {
83
        $loggerClosure = function ($message, $msgType = 'info') use ($output) {
84
            $time = \date('Y-m-d G:i:s');
85
            $message = \sprintf(
86
                    '<comment>%s | Mem cur/peak: %dm/%dm </comment> | <'.$msgType.'>%s</'.$msgType.'>',
87
                    $time,
88
                    \round(\memory_get_usage(true) / 1024 / 1024, 1),
89
                    \round(\memory_get_peak_usage(true) / 1024 / 1024, 1),
90
                    $message
91
                );
92
            $output->writeln($message);
93
        };
94
95
        return $loggerClosure;
96
    }
97
}
98