Completed
Pull Request — master (#1258)
by
unknown
01:33
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
namespace Liip\ImagineBundle\Command;
4
5
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
6
use Liip\ImagineBundle\Imagine\Cache\CacheWarmer;
7
use Liip\ImagineBundle\Imagine\Data\DataManager;
8
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * A command to warm up images cache (i.e. pre-generate thumbnails)
17
 *
18
 * @author Konstantin Tjuterev <[email protected]>
19
 */
20
class WarmCacheCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
21
{
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('liip:imagine:cache:warm')
26
            ->setDescription('Warms cache for paths provided by given warmers (or all warmers, if run w/o params)')
27
            ->addOption('chunk-size', 'c', InputOption::VALUE_REQUIRED, 'Chunk size', 100)
28
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force cache warm up for already cached images')
29
            ->addArgument('warmers', InputArgument::OPTIONAL|InputArgument::IS_ARRAY, 'Warmers names')
30
            ->setHelp(<<<EOF
31
The <info>%command.name%</info> command warms up cache by specified parameters.
32
33
A warmer can be configured for one or more filter set. A warmer should return a list of paths.
34
This command gets the paths from warmer and create cache (i.e. filtered image) for each filter configured for given warmer.
35
36
Warmers should be separated by spaces:
37
<info>php app/console %command.name% warmer1 warmer2</info>
38
All cache for a given `warmers` will be warmed up
39
40
<info>php app/console %command.name%</info>
41
Cache for all warmers will be warmed up when executing this command without parameters.
42
43
Note, that <info>--force</info> option will force regeneration of the cache only if warmer returns the path.
44
Generally, there should be NO need to use this option, instead, use <info>liip:imagine:cache:remove</info> command to clear cache.
45
Then run this command to warm-up the cache
46
EOF
47
            );
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $warmers = $input->getArgument('warmers');
53
54
        /** @var CacheWarmer $cacheWarmer */
55
        $cacheWarmer = $this->getContainer()->get('liip_imagine.cache.warmer');
56
        $cacheWarmer->setLoggerClosure($this->getLoggerClosure($output));
57
58
        if ($chunkSize = $input->getOption('chunk-size')) {
59
            $cacheWarmer->setChunkSize($chunkSize);
60
        }
61
62
        $force = false;
63
        if ($input->getOption('force')) {
64
            $force = true;
65
        }
66
67
        $cacheWarmer->warm($force, $warmers);
0 ignored issues
show
Bug introduced by
It seems like $warmers defined by $input->getArgument('warmers') on line 52 can also be of type string; however, Liip\ImagineBundle\Imagi...che\CacheWarmer::warm() does only seem to accept null|array, 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...
68
    }
69
70
    /**
71
     * Returns Logger Closure
72
     *
73
     * @return callable
74
     */
75
    protected function getLoggerClosure(OutputInterface $output)
76
    {
77
        $loggerClosure = function ($message, $msgType = 'info') use ($output) {
78
                $time = date('Y-m-d G:i:s');
79
                $message = sprintf(
80
                    '<comment>%s | Mem cur/peak: %dm/%dm </comment> | <' . $msgType . '>%s</' . $msgType . '>',
81
                    $time,
82
                    round(memory_get_usage(true) / 1024 / 1024, 1),
83
                    round(memory_get_peak_usage(true) / 1024 / 1024, 1),
84
                    $message
85
                );
86
                $output->writeln($message);
87
        };
88
        return $loggerClosure;
89
    }
90
}
91