Completed
Pull Request — 1.0 (#967)
by Rob
02:13
created

ResolveCacheCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 10
dl 0
loc 150
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 46 1
C execute() 0 41 8
A outputTitle() 0 7 1
B outputSummary() 0 17 5
A getFilterManager() 0 4 1
A getDataManager() 0 4 1
A getCacheManager() 0 4 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 Liip\ImagineBundle\Imagine\Data\DataManager;
16
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
17
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
class ResolveCacheCommand extends ContainerAwareCommand
24
{
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('liip:imagine:cache:resolve')
29
            ->setDescription('Resolve cache for given path and set of filters.')
30
            ->setDefinition(array(
31
                new InputArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY,
32
                    'Any number of image paths to act on.'),
33
                new InputOption('filters', ['f'], InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
34
                    'List of filters to apply to passed images.'),
35
                new InputOption('force', ['F'], InputOption::VALUE_NONE,
36
                    'Force image resolution regardless of cache.'),
37
                new InputOption('machine', ['m'], InputOption::VALUE_NONE,
38
                    'Only print machine-parseable results.'),
39
            ))
40
            ->setHelp(<<<'EOF'
41
The <comment>%command.name%</comment> command resolves the passed image(s) for the resolved
42
filter(s), outputting results using the following basic format:
43
  <info>- "image.ext[filter]" (resolved|cached) as "path/to/cached/image.ext"</>
44
45
46
<comment># bin/console %command.name% --filters=thumb1 foo.ext bar.ext</comment>
47
Resolve <options=bold>both</> <comment>foo.ext</comment> and <comment>bar.ext</comment> using <comment>thumb1</comment> filter, outputting:
48
  <info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</>
49
  <info>- "bar.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/bar.ext"</>
50
51
52
<comment># bin/console %command.name% --filters=thumb1 --filters=thumb2 foo.ext</comment>
53
Resolve <comment>foo.ext</comment> using <options=bold>both</> <comment>thumb1</comment> and <comment>thumb2</comment> filters, outputting:
54
  <info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</>
55
  <info>- "foo.ext[thumb2]" resolved as "http://localhost/media/cache/thumb2/foo.ext"</>
56
57
58
<comment># bin/console %command.name% foo.ext</comment>
59
Resolve <comment>foo.ext</comment> using <options=bold>all configured filters</> (as none are specified), outputting:
60
  <info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</>
61
  <info>- "foo.ext[thumb2]" resolved as "http://localhost/media/cache/thumb2/foo.ext"</>
62
63
64
<comment># bin/console %command.name% --force --filters=thumb1 foo.ext</comment>
65
Resolve <comment>foo.ext</comment> using <comment>thumb1</comment> and <options=bold>force creation</> regardless of cache, outputting:
66
  <info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</>
67
68
EOF
69
            );
70
    }
71
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        $imagePaths = $input->getArgument('paths');
75
        $useFilters = $input->getOption('filters');
76
        $forced = $input->getOption('force');
77
        $failures = 0;
78
79
        $filterManager = $this->getFilterManager();
80
        $dataManager = $this->getDataManager();
81
        $cacheManager = $this->getCacheManager();
82
83
        if (0 === count($useFilters)) {
84
            $useFilters = array_keys($filterManager->getFilterConfiguration()->all());
85
        }
86
87
        $this->outputTitle($output);
88
89
        foreach ($imagePaths as $path) {
90
            foreach ($useFilters as $filter) {
91
                $output->write(sprintf('- "%s[%s]" ', $path, $filter));
92
93
                try {
94
                    if ($forced || !$cacheManager->isStored($path, $filter)) {
95
                        $cacheManager->store($filterManager->applyFilter($dataManager->find($filter, $path), $filter), $path, $filter);
96
                        $output->write('RESOLVED ');
97
                    } else {
98
                        $output->write('CACHED ');
99
                    }
100
101
                    $output->writeln(sprintf('as "%s"', $cacheManager->resolve($path, $filter)));
102
                } catch (\Exception $e) {
103
                    $output->writeln(sprintf('FAILED with exception "%s"', $e->getMessage()));
104
                    ++$failures;
105
                }
106
            }
107
        }
108
109
        $this->outputSummary($output, $useFilters, $imagePaths, $failures);
110
111
        return 0 === $failures ? 0 : 255;
112
    }
113
114
    /**
115
     * @param OutputInterface $output
116
     */
117
    private function outputTitle(OutputInterface $output)
118
    {
119
        $title = 'Resolving Imagine Bundle Images';
120
        $output->writeln(sprintf('<info>%s</info>', $title));
121
        $output->writeln(str_repeat('=', strlen($title)));
122
        $output->writeln('');
123
    }
124
125
    /**
126
     * @param OutputInterface $output
127
     * @param string[]        $useFilters
128
     * @param string[]        $imagePaths
129
     * @param int             $failures
130
     */
131
    private function outputSummary(OutputInterface $output, $useFilters, $imagePaths, $failures)
132
    {
133
        $useFiltersCount = count($useFilters);
134
        $imagePathsCount = count($imagePaths);
135
        $totalActionStep = $useFiltersCount * $imagePathsCount;
136
137
        $output->writeln('');
138
        $output->writeln(vsprintf('Completed %d %s (%d %s on %d %s) %s', [
139
            $totalActionStep,
140
            1 === $totalActionStep ? 'operation' : 'operations',
141
            count($useFilters),
142
            1 === $useFiltersCount ? 'filter' : 'filters',
143
            count($imagePaths),
144
            1 === $totalActionStep ? 'image' : 'images',
145
            0 === $failures ? '' : sprintf('<fg=red>[encountered</> <fg=red;options=bold>%d</> <fg=red> failures]</>', $failures)
146
        ]));
147
    }
148
149
    /**
150
     * @return FilterManager
151
     */
152
    private function getFilterManager()
153
    {
154
        return $this->getContainer()->get('liip_imagine.filter.manager');
155
    }
156
157
    /**
158
     * @return DataManager
159
     */
160
    private function getDataManager()
161
    {
162
        return $this->getContainer()->get('liip_imagine.data.manager');
163
    }
164
165
    /**
166
     * @return CacheManager
167
     */
168
    private function getCacheManager()
169
    {
170
        return $this->getContainer()->get('liip_imagine.cache.manager');
171
    }
172
}
173