Completed
Pull Request — 1.0 (#967)
by Rob
01:52
created

RemoveCacheCommand::resolveInputFilters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class RemoveCacheCommand extends ContainerAwareCommand
22
{
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('liip:imagine:cache:remove')
27
            ->setDescription('Remove cache for given paths and set of filters.')
28
            ->addArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Image paths')
29
            ->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
30
                'List of filters to remove for passed images (Deprecated, use "filter").')
31
            ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
32
                'List of filters to remove for passed images.')
33
            ->setHelp(<<<'EOF'
34
The <info>%command.name%</info> command removes cache by specified parameters.
35
36
Paths should be separated by spaces:
37
<info>php app/console %command.name% path1 path2</info>
38
All cache for a given `paths` will be lost.
39
40
If you use --filter parameter:
41
<info>php app/console %command.name% --filter=thumb1 --filter=thumb2</info>
42
All cache for a given filters will be lost.
43
44
You can combine these parameters:
45
<info>php app/console %command.name% path1 path2 --filter=thumb1 --filter=thumb2</info>
46
47
<info>php app/console %command.name%</info>
48
Cache for all paths and filters will be lost when executing this command without parameters.
49
EOF
50
            );
51
    }
52
53
    /**
54
     * @param InputInterface  $input
55
     * @param OutputInterface $output
56
     *
57
     * @return int
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $paths = $input->getArgument('paths');
62
        $filters = $this->resolveInputFilters($input);
63
64
        if (empty($filters)) {
65
            $filters = null;
66
        }
67
68
        /* @var CacheManager cacheManager */
69
        $cacheManager = $this->getContainer()->get('liip_imagine.cache.manager');
70
        $cacheManager->remove($paths, $filters);
71
72
        return 0;
73
    }
74
75
    /**
76
     * @param InputInterface $input
77
     *
78
     * @return array|mixed
79
     */
80 View Code Duplication
    private function resolveInputFilters(InputInterface $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $filters = $input->getOption('filter');
83
84
        if (count($filtersDeprecated = $input->getOption('filters'))) {
85
            $filters = array_merge($filters, $filtersDeprecated);
86
            @trigger_error('As of 1.9, use of the "--filters" option has been deprecated in favor of "--filter" and will be removed in 2.0.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
        }
88
89
        return $filters;
90
    }
91
}
92