Completed
Push — quiet-mode ( e918d9 )
by Arnaud
03:38
created

AbstractCommand::newPB()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of the PHPoole package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PHPoole\Command;
12
13
use PHPoole\PHPoole;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Component\Yaml\Exception\ParseException;
16
use Symfony\Component\Yaml\Yaml;
17
use Zend\Console\Adapter\AdapterInterface as Console;
18
use Zend\Console\ColorInterface as Color;
19
use Zend\ProgressBar\ProgressBar;
20
use ZF\Console\Route;
21
22
abstract class AbstractCommand
23
{
24
    const CONFIG_FILE = 'phpoole.yml';
25
26
    /**
27
     * @var Console
28
     */
29
    protected $console;
30
    /**
31
     * @var Route
32
     */
33
    protected $route;
34
    /**
35
     * @var string
36
     */
37
    protected $path;
38
    /**
39
     * @var PHPoole
40
     */
41
    protected $phpoole;
42
    /**
43
     * @var Filesystem
44
     */
45
    protected $fs;
46
    /**
47
     * @var ProgressBar
48
     */
49
    protected $progressBar = null;
50
    /**
51
     * @var int
52
     */
53
    protected $pbMax = 0;
54
    /**
55
     * @var bool
56
     */
57
    protected $debug = false;
58
    /**
59
     * @var bool
60
     */
61
    protected $quiet = false;
62
63
    /**
64
     * Start command processing.
65
     *
66
     * @param Route   $route
67
     * @param Console $console
68
     *
69
     * @return mixed
70
     */
71
    public function __invoke(Route $route, Console $console)
72
    {
73
        $this->route = $route;
74
        $this->console = $console;
75
76
        $this->path = realpath($this->route->getMatchedParam('path', getcwd()));
77
        if (!is_dir($this->path)) {
78
            throw new \Exception('Invalid <path> provided!');
79
        }
80
        $this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path);
81
82
        $this->fs = new Filesystem();
83
84
        return $this->processCommand();
85
    }
86
87
    /**
88
     * Process the command.
89
     */
90
    abstract public function processCommand();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
91
92
    /**
93
     * @return Console
94
     */
95
    public function getConsole()
96
    {
97
        return $this->console;
98
    }
99
100
    /**
101
     * @return Route
102
     */
103
    public function getRoute()
104
    {
105
        return $this->route;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getPath()
112
    {
113
        return $this->path;
114
    }
115
116
    /**
117
     * @param int $start
118
     * @param int $max
119
     *
120
     * @return ProgressBar
121
     */
122
    protected function newPB($start, $max)
123
    {
124
        if ($this->progressBar === null || $max != $this->pbMax) {
125
            $this->pbMax = $max;
126
            $adapter = new \Zend\ProgressBar\Adapter\Console([
127
                'elements' => [
128
                    \Zend\ProgressBar\Adapter\Console::ELEMENT_PERCENT,
129
                    \Zend\ProgressBar\Adapter\Console::ELEMENT_BAR,
130
                    \Zend\ProgressBar\Adapter\Console::ELEMENT_TEXT,
131
                ],
132
                'textWidth' => 30,
133
            ]);
134
            $this->progressBar = new ProgressBar($adapter, $start, $max);
135
        }
136
137
        return $this->progressBar;
138
    }
139
140
    /**
141
     * @return ProgressBar
142
     */
143
    protected function getPB()
144
    {
145
        return $this->progressBar;
146
    }
147
148
    /**
149
     * @param array $options
150
     *
151
     * @return PHPoole
152
     */
153
    public function getPHPoole(array $options = [])
154
    {
155
        // debug mode?
156
        if (array_key_exists('debug', $options) && $options['debug']) {
157
            $this->debug = true;
158
        }
159
        // quiet mode?
160
        if (array_key_exists('quiet', $options) && $options['quiet']) {
161
            $this->quiet = true;
162
        }
163
164
        // CLI custom message callback function
165
        $messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
166
            switch ($code) {
167
                case 'CREATE':
168
                case 'CONVERT':
169
                case 'GENERATE':
170
                case 'MENU':
171
                case 'COPY':
172
                case 'RENDER':
173
                case 'TIME':
174
                    $this->wlAnnonce($message);
175
                    break;
176
                case 'CREATE_PROGRESS':
177
                case 'CONVERT_PROGRESS':
178
                case 'GENERATE_PROGRESS':
179
                case 'MENU_PROGRESS':
180
                case 'COPY_PROGRESS':
181
                case 'RENDER_PROGRESS':
182
                    if ($this->debug) {
183
                        if ($itemsCount > 0) {
184
                            $this->wlDone(sprintf("(%u/%u) %s", $itemsCount, $itemsMax, $message));
185
                            break;
186
                        }
187
                        $this->wlDone("$message");
188
                    } else {
189
                        if (!$this->quiet) {
190
                            if ($itemsMax && $itemsCount) {
191
                                $this->newPB(1, $itemsMax);
192
                                $this->getPB()->update($itemsCount, "$message");
193
                                if ($itemsCount == $itemsMax) {
194
                                    $this->getPB()->update($itemsCount, "[$itemsCount/$itemsMax]");
195
                                    $this->getPB()->finish();
196
                                }
197
                            } else {
198
                                $this->wl($message);
199
                            }
200
                        }
201
                    }
202
                    break;
203
                case 'CREATE_ERROR':
204
                case 'CONVERT_ERROR':
205
                case 'GENERATE_ERROR':
206
                case 'MENU_ERROR':
207
                case 'COPY_ERROR':
208
                case 'RENDER_ERROR':
209
                    $this->wlError($message);
210
                    break;
211
            }
212
        };
213
214
        // instanciate PHPoole?
215
        if (!$this->phpoole instanceof PHPoole) {
216
            if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) {
217
                throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath()));
218
            }
219
220
            try {
221
                $optionsFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE));
222
                if (is_array($options)) {
223
                    $options = array_replace_recursive($optionsFile, $options);
224
                }
225
                $this->phpoole = new PHPoole($options, $messageCallback);
226
                $this->phpoole->setSourceDir($this->getPath());
227
                $this->phpoole->setDestinationDir($this->getPath());
228
            } catch (ParseException $e) {
229
                throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage()));
230
            } catch (\Exception $e) {
231
                throw new \Exception(sprintf($e->getMessage()));
232
            }
233
        }
234
235
        return $this->phpoole;
236
    }
237
238
    /**
239
     * @param string $text
240
     */
241
    public function wl($text)
242
    {
243
        $this->console->writeLine($text);
244
    }
245
246
    /**
247
     * @param string $text
248
     */
249
    public function wlAnnonce($text)
250
    {
251
        $this->console->writeLine($text, Color::WHITE);
252
    }
253
254
    /**
255
     * @param string $text
256
     */
257
    public function wlDone($text)
258
    {
259
        $this->console->writeLine($text, Color::GREEN);
260
    }
261
262
    /**
263
     * @param string $text
264
     */
265
    public function wlAlert($text)
266
    {
267
        $this->console->writeLine($text, Color::YELLOW);
268
    }
269
270
    /**
271
     * @param string $text
272
     */
273
    public function wlError($text)
274
    {
275
        $this->console->writeLine($text, Color::RED);
276
    }
277
}
278