Completed
Pull Request — 2.0 (#981)
by Rob
12:38
created

RemoveCacheCommand::runCacheImageRemove()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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