Completed
Push — master ( f05361...a0ad3b )
by David
02:06 queued 11s
created

RemoveCacheCommand::runCacheImageRemove()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
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\CacheManager;
15
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class RemoveCacheCommand extends Command
23
{
24
    protected static $defaultName = 'liip:imagine:cache:remove';
25
26
    use CacheCommandTrait;
27
28
    public function __construct(CacheManager $cacheManager, FilterManager $filterManager)
29
    {
30
        parent::__construct();
31
32
        $this->cacheManager = $cacheManager;
33
        $this->filterManager = $filterManager;
34
    }
35
36
    protected function configure(): void
37
    {
38
        $this
39
            ->setDescription('Remove cache entries for given paths and filters.')
40
            ->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY,
41
                'Image file path(s) to run resolution on.')
42
            ->addOption('filter', 'f', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
43
                'Filter(s) to use for image remove; if none explicitly passed, use all filters.')
44
            ->addOption('no-colors', 'C', InputOption::VALUE_NONE,
45
                'Write only un-styled text output; remove any colors, styling, etc.')
46
            ->addOption('as-script', 'S', InputOption::VALUE_NONE,
47
                'Write only machine-readable output; silenced verbose reporting and implies --no-colors.')
48
            ->setHelp(<<<'EOF'
49
The <comment>%command.name%</comment> command removes the passed image(s) cache entry for the 
50
resolved filter(s), outputting results using the following basic format:
51
  <info>image.ext[filter] (removed|skipped|failure)[: (image-path|exception-message)]</>
52
53
<comment># bin/console %command.name% --filter=thumb1 foo.ext bar.ext</comment>
54
Remove cache for <options=bold>both</> <comment>foo.ext</comment> and <comment>bar.ext</comment> images for <options=bold>one</> filter (<comment>thumb1</comment>), outputting:
55
  <info>- foo.ext[thumb1] removed</>
56
  <info>- bar.ext[thumb1] removed</>
57
58
<comment># bin/console %command.name% --filter=thumb1 --filter=thumb3 foo.ext</comment>
59
Remove cache for <comment>foo.ext</comment> image using <options=bold>two</> filters (<comment>thumb1</comment> and <comment>thumb3</comment>), outputting:
60
  <info>- foo.ext[thumb1] removed</>
61
  <info>- foo.ext[thumb3] removed</>
62
63
<comment># bin/console %command.name% foo.ext</comment>
64
Remove cache for <comment>foo.ext</comment> image using <options=bold>all</> filters (as none were specified), outputting:
65
  <info>- foo.ext[thumb1] removed</>
66
  <info>- foo.ext[thumb2] removed</>
67
  <info>- foo.ext[thumb3] removed</>
68
69
EOF
70
            );
71
    }
72
73
    protected function execute(InputInterface $input, OutputInterface $output): int
74
    {
75
        $this->setupOutputStyle($input, $output);
76
        $this->outputCommandHeader();
77
78
        [$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...
79
80
        foreach ($images as $i) {
81
            foreach ($filters as $f) {
82
                $this->runCacheImageRemove($i, $f);
83
            }
84
        }
85
86
        $this->outputCommandResult($images, $filters, 'removal');
87
88
        return $this->getResultCode();
89
    }
90
91
    private function runCacheImageRemove(string $image, string $filter): void
92
    {
93
        if (!$this->outputMachineReadable) {
94
            $this->io->text(' - ');
95
        }
96
97
        $this->io->group($image, $filter, 'blue');
98
99
        if ($this->cacheManager->isStored($image, $filter)) {
100
            $this->cacheManager->remove($image, $filter);
101
            $this->io->status('removed', 'green');
102
        } else {
103
            $this->io->status('skipped', 'yellow');
104
        }
105
106
        $this->io->newline();
107
    }
108
}
109