ResolveCacheCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
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\CacheManager;
15
use Liip\ImagineBundle\Imagine\Data\DataManager;
16
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class ResolveCacheCommand extends Command
24
{
25
    use CacheCommandTrait;
26
    protected static $defaultName = 'liip:imagine:cache:resolve';
27
28
    /**
29
     * @var DataManager
30
     */
31
    private $dataManager;
32
33
    public function __construct(DataManager $dataManager, CacheManager $cacheManager, FilterManager $filterManager)
34
    {
35
        parent::__construct();
36
37
        $this->dataManager = $dataManager;
38
        $this->cacheManager = $cacheManager;
39
        $this->filterManager = $filterManager;
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this
45
            ->setDescription('Warms up the cache for the specified image sources with all or specified filters applied, and prints the list of cache files.')
46
            ->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY,
47
                'Image file path(s) for which to generate the cached images.')
48
            ->addOption('filter', 'f', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
49
                'Filter(s) to use for image resolution; if none explicitly passed, use all filters.')
50
            ->addOption('force', 'F', InputOption::VALUE_NONE,
51
                'Force generating the image and writing the cache, regardless of whether a cached version already exists.')
52
            ->addOption('no-colors', 'C', InputOption::VALUE_NONE,
53
                'Write only un-styled text output; remove any colors, styling, etc.')
54
            ->addOption('as-script', 'S', InputOption::VALUE_NONE,
55
                'Write only machine-readable output; silenced verbose reporting and implies --no-colors.')
56
            ->setHelp(<<<'EOF'
57
The <comment>%command.name%</comment> command resolves the passed image(s) for the resolved
58
filter(s), outputting results using the following basic format:
59
  <info>image.ext[filter] (resolved|cached|failed): (resolve-image-path|exception-message)</>
60
61
<comment># bin/console %command.name% --filter=thumb1 foo.ext bar.ext</comment>
62
Resolve <options=bold>both</> <comment>foo.ext</comment> and <comment>bar.ext</comment> images using <options=bold>one</> filter (<comment>thumb1</comment>), outputting:
63
  <info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</>
64
  <info>- bar.ext[thumb1] status: http://localhost/media/cache/thumb1/bar.ext</>
65
66
<comment># bin/console %command.name% --filter=thumb1 --filter=thumb3 foo.ext</comment>
67
Resolve <comment>foo.ext</comment> image using <options=bold>two</> filters (<comment>thumb1</comment> and <comment>thumb3</comment>), outputting:
68
  <info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</>
69
  <info>- foo.ext[thumb3] status: http://localhost/media/cache/thumb3/foo.ext</>
70
71
<comment># bin/console %command.name% foo.ext</comment>
72
Resolve <comment>foo.ext</comment> image using <options=bold>all</> filters (as none were specified), outputting:
73
  <info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</>
74
  <info>- foo.ext[thumb2] status: http://localhost/media/cache/thumb2/foo.ext</>
75
  <info>- foo.ext[thumb3] status: http://localhost/media/cache/thumb2/foo.ext</>
76
77
<comment># bin/console %command.name% --force --filter=thumb1 foo.ext</comment>
78
Resolve <comment>foo.ext</comment> image using <options=bold>one</> filter (<comment>thumb1</comment>) and <options=bold>forcing resolution</> (regardless of cache), outputting:
79
  <info>- foo.ext[thumb1] resolved: http://localhost/media/cache/thumb1/foo.ext</>
80
81
EOF
82
            );
83
    }
84
85
    protected function execute(InputInterface $input, OutputInterface $output): int
86
    {
87
        $this->setupOutputStyle($input, $output);
88
        $this->outputCommandHeader();
89
90
        $forced = $input->getOption('force');
91
        [$images, $filters] = $this->resolveInputFiltersAndPaths($input);
0 ignored issues
show
Bug introduced by
The variable $images does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $filters does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
92
93
        foreach ($images as $i) {
94
            foreach ($filters as $f) {
95
                $this->runCacheImageResolve($i, $f, $forced);
96
            }
97
        }
98
99
        $this->outputCommandResult($images, $filters, 'resolution');
100
101
        return $this->getResultCode();
102
    }
103
104
    private function runCacheImageResolve(string $image, string $filter, bool $forced): void
105
    {
106
        if (!$this->outputMachineReadable) {
107
            $this->io->text(' - ');
108
        }
109
110
        $this->io->group($image, $filter, 'blue');
111
112
        try {
113
            if ($forced || !$this->cacheManager->isStored($image, $filter)) {
114
                $this->cacheManager->store($this->filterManager->applyFilter($this->dataManager->find($filter, $image), $filter), $image, $filter);
115
                $this->io->status('resolved', 'green');
116
            } else {
117
                $this->io->status('cached', 'white');
118
            }
119
120
            $this->io->line(sprintf(' %s', $this->cacheManager->resolve($image, $filter)));
121
        } catch (\Exception $e) {
122
            ++$this->failures;
123
124
            $this->io->status('failed', 'red');
125
            $this->io->line(' %s', [$e->getMessage()]);
126
        }
127
    }
128
}
129