Completed
Pull Request — master (#1213)
by
unknown
01:34
created

ResolveCacheCommand::runCacheImageResolve()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.2248
c 0
b 0
f 0
cc 5
nc 16
nop 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\Filter\FilterManager;
15
use Liip\ImagineBundle\Service\FilterService;
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 ResolveCacheCommand extends Command
23
{
24
    /* @var FilterManager $filterManager */
25
    private $filterManager;
26
27
    /* @var FilterService $filterService */
28
    private $filterService;
29
30
    public function __construct(FilterManager $filterManager, FilterService $filterService)
31
    {
32
        $this->filterManager = $filterManager;
33
        $this->filterService = $filterService;
34
35
        parent::__construct('liip:imagine:cache:resolve');
36
    }
37
38 View Code Duplication
    protected function configure()
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...
39
    {
40
        $this
41
            ->setDescription('Resolve cache for given path and set of filters.')
42
            ->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Image paths')
43
            ->addOption(
44
                'filters',
45
                'f',
46
                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
47
                'Filters list'
48
            )->setHelp(<<<'EOF'
49
The <info>%command.name%</info> command resolves cache by specified parameters.
50
It returns list of urls.
51
52
<info>php app/console %command.name% path1 path2 --filters=thumb1</info>
53
Cache for this two paths will be resolved with passed filter.
54
As a result you will get<info>
55
    http://localhost/media/cache/thumb1/path1
56
    http://localhost/media/cache/thumb1/path2</info>
57
58
You can pass few filters:
59
<info>php app/console %command.name% path1 --filters=thumb1 --filters=thumb2</info>
60
As a result you will get<info>
61
    http://localhost/media/cache/thumb1/path1
62
    http://localhost/media/cache/thumb2/path1</info>
63
64
If you omit --filters parameter then to resolve given paths will be used all configured and available filters in application:
65
<info>php app/console %command.name% path1</info>
66
As a result you will get<info>
67
    http://localhost/media/cache/thumb1/path1
68
    http://localhost/media/cache/thumb2/path1</info>
69
EOF
70
            );
71
    }
72
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $paths = $input->getArgument('paths');
76
        $filters = $input->getOption('filters');
77
78
        if (empty($filters)) {
79
            $filters = array_keys($this->filterManager->getFilterConfiguration()->all());
80
        }
81
82
        foreach ($paths as $path) {
0 ignored issues
show
Bug introduced by
The expression $paths of type string|array<integer,string>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
83
            foreach ($filters as $filter) {
0 ignored issues
show
Bug introduced by
The expression $filters of type string|boolean|array<integer,integer|string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
84
                $output->writeln($this->filterService->getUrlOfFilteredImage($path, $filter));
85
            }
86
        }
87
    }
88
}
89