Completed
Push — upgrade-2.0-from-1.0 ( 918602 )
by David
13:04
created

AbstractCacheCommand::getCacheManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
abstract class AbstractCacheCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * @var OutputInterface
25
     */
26
    protected $output;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $machineReadable;
32
33
    /**
34
     * @var int
35
     */
36
    protected $actionFailures;
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     */
42
    protected function initializeInstState(InputInterface $input, OutputInterface $output)
43
    {
44
        $this->output = $output;
45
        $this->machineReadable = $input->getOption('machine-readable');
46
        $this->actionFailures = 0;
47
    }
48
49
    /**
50
     * @param InputInterface $input
51
     *
52
     * @return array
53
     */
54
    protected function resolveFilters(InputInterface $input)
55
    {
56
        $filters = $input->getOption('filter');
57
58
        if (0 !== count($deprecated = $input->getOption('filters'))) {
59
            $filters = array_merge($filters, $deprecated);
60
            @trigger_error('The --filters option was deprecated in 1.9.0 and removed in 2.0.0. Use the --filter option instead.', 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...
61
        }
62
63
        if (0 === count($filters) && 0 === count($filters = array_keys($this->getFilterManager()->getFilterConfiguration()->all()))) {
64
            $this->output->writeln('<bg=red;fg=white> [ERROR] You do not have any configured filters available. </>');
65
        }
66
67
        return $filters;
68
    }
69
70
    /**
71
     * @param string $command
72
     */
73
    protected function writeCommandHeading($command)
74
    {
75
        if ($this->machineReadable) {
76
            return;
77
        }
78
79
        $title = sprintf('[liip/imagine-bundle] %s Image Caches', ucfirst($command));
80
        $this->writeNewline();
81
        $this->output->writeln(sprintf('<info>%s</info>', $title));
82
        $this->output->writeln(str_repeat('=', strlen($title)));
83
        $this->writeNewline();
84
    }
85
86
    /**
87
     * @param string[] $filters
88
     * @param string[] $targets
89
     * @param bool     $glob
90
     */
91
    protected function writeResultSummary(array $filters, array $targets, $glob = false)
92
    {
93
        if ($this->machineReadable) {
94
            return;
95
        }
96
97
        $targetCount = count($targets);
98
        $filterCount = count($filters);
99
        $actionCount = ($glob ? $filterCount : ($filterCount * $targetCount)) - $this->actionFailures;
100
101
        $this->writeNewline();
102
        $this->output->writeln(vsprintf('<fg=black;bg=green> Completed %d %s (%d %s / %s %s) </>%s', array(
103
            $actionCount,
104
            $this->getPluralized($actionCount, 'operation'),
105
            $filterCount,
106
            $this->getPluralized($filterCount, 'filter'),
107
            $glob ? '?' : $targetCount,
108
            $this->getPluralized($targetCount, 'image'),
109
            $this->getResultSummaryFailureMarkup(),
110
        )));
111
        $this->writeNewline();
112
    }
113
114
    /**
115
     * @param string      $filter
116
     * @param string|null $target
117
     */
118
    protected function writeActionStart($filter, $target = null)
119
    {
120
        if (!$this->machineReadable) {
121
            $this->output->write(' - ');
122
        }
123
124
        $this->output->write(sprintf('%s[%s] ', $target ?: '*', $filter));
125
    }
126
127
    /**
128
     * @param string $result
129
     * @param bool   $continued
130
     */
131
    protected function writeActionResult($result, $continued = true)
132
    {
133
        $this->output->write($continued ? sprintf('%s: ', $result) : $result);
134
135
        if (!$continued) {
136
            $this->writeNewline();
137
        }
138
    }
139
140
    /**
141
     * @param string $detail
142
     */
143
    protected function writeActionDetail($detail)
144
    {
145
        $this->output->write($detail);
146
        $this->writeNewline();
147
    }
148
149
    /**
150
     * @param \Exception $exception
151
     */
152
    protected function writeActionException(\Exception $exception)
153
    {
154
        $this->writeActionResult('failure');
155
        $this->writeActionDetail($exception->getMessage());
156
        ++$this->actionFailures;
157
    }
158
159
    /**
160
     * @return int
161
     */
162
    protected function getReturnCode()
163
    {
164
        return 0 === $this->actionFailures ? 0 : 255;
165
    }
166
167
    /**
168
     * @return CacheManager
169
     */
170
    protected function getCacheManager()
171
    {
172
        static $manager;
173
174
        if (null === $manager) {
175
            $manager = $this->getContainer()->get('liip_imagine.cache.manager');
176
        }
177
178
        return $manager;
179
    }
180
181
    /**
182
     * @return FilterManager
183
     */
184
    protected function getFilterManager()
185
    {
186
        static $manager;
187
188
        if (null === $manager) {
189
            $manager = $this->getContainer()->get('liip_imagine.filter.manager');
190
        }
191
192
        return $manager;
193
    }
194
195
    /**
196
     * @return DataManager
197
     */
198
    protected function getDataManager()
199
    {
200
        static $manager;
201
202
        if (null === $manager) {
203
            $manager = $this->getContainer()->get('liip_imagine.data.manager');
204
        }
205
206
        return $manager;
207
    }
208
209
    /**
210
     * @param int $count
211
     */
212
    private function writeNewline($count = 1)
213
    {
214
        $this->output->write(str_repeat(PHP_EOL, $count));
215
    }
216
217
    /**
218
     * @param int    $size
219
     * @param string $word
220
     *
221
     * @return string
222
     */
223
    private function getPluralized($size, $word)
224
    {
225
        return 1 === $size ? $word : sprintf('%ss', $word);
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    private function getResultSummaryFailureMarkup()
232
    {
233
        if (0 === $this->actionFailures) {
234
            return '';
235
        }
236
237
        return vsprintf(' <fg=white;bg=red;options=bold> encountered %s %s </>', array(
238
            $this->actionFailures,
239
            $this->getPluralized($this->actionFailures, 'failure'),
240
        ));
241
    }
242
}
243