RemoveCacheCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 0
loc 90
rs 10
c 0
b 0
f 0

4 Methods

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