Completed
Push — new-dir ( 249da3 )
by Arnaud
01:43
created

AbstractCommand::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
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\Console\Prompt\Confirm;
20
use Zend\ProgressBar\ProgressBar;
21
use ZF\Console\Route;
22
23
abstract class AbstractCommand
24
{
25
    const CONFIG_FILE = 'phpoole.yml';
26
27
    /**
28
     * @var Console
29
     */
30
    protected $console;
31
    /**
32
     * @var Route
33
     */
34
    protected $route;
35
    /**
36
     * @var string
37
     */
38
    protected $path;
39
    /**
40
     * @var PHPoole
41
     */
42
    protected $phpoole;
43
    /**
44
     * @var Filesystem
45
     */
46
    protected $fs;
47
    /**
48
     * @var ProgressBar
49
     */
50
    protected $progressBar = null;
51
    /**
52
     * @var int
53
     */
54
    protected $pbMax = 0;
55
    /**
56
     * @var bool
57
     */
58
    protected $debug = false;
59
    /**
60
     * @var bool
61
     */
62
    protected $quiet = false;
63
64
    /**
65
     * Start command processing.
66
     *
67
     * @param Route   $route
68
     * @param Console $console
69
     *
70
     * @return mixed
71
     */
72
    public function __invoke(Route $route, Console $console)
73
    {
74
        $this->route = $route;
75
        $this->console = $console;
76
        $this->fs = new Filesystem();
77
78
        $this->path = $this->route->getMatchedParam('path', getcwd());
79
80
        if (realpath($this->path) === false) {
81
            if ($this->getRoute()->getName() != 'new') {
82
                throw new \Exception('Invalid <path> provided!');
83
            }
84
            if (!Confirm::prompt('The provided <path> doesn\'t exist. Do you want to create it? [y/n]', 'y', 'n')) {
85
                exit(0);
86
            }
87
            $this->fs->mkdir($this->path);
88
        }
89
        $this->path = realpath($this->path);
90
        $this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path);
91
92
        return $this->processCommand();
93
    }
94
95
    /**
96
     * Process the command.
97
     */
98
    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...
99
100
    /**
101
     * @return Console
102
     */
103
    public function getConsole()
104
    {
105
        return $this->console;
106
    }
107
108
    /**
109
     * @return Route
110
     */
111
    public function getRoute()
112
    {
113
        return $this->route;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getPath()
120
    {
121
        return $this->path;
122
    }
123
124
    /**
125
     * @param int $start
126
     * @param int $max
127
     *
128
     * @return ProgressBar
129
     */
130
    protected function newPB($start, $max)
131
    {
132
        if ($this->progressBar === null || $max != $this->pbMax) {
133
            $this->pbMax = $max;
134
            $adapter = new \Zend\ProgressBar\Adapter\Console([
135
                'elements' => [
136
                    \Zend\ProgressBar\Adapter\Console::ELEMENT_PERCENT,
137
                    \Zend\ProgressBar\Adapter\Console::ELEMENT_BAR,
138
                    \Zend\ProgressBar\Adapter\Console::ELEMENT_TEXT,
139
                ],
140
                'textWidth' => 30,
141
            ]);
142
            $this->progressBar = new ProgressBar($adapter, $start, $max);
143
        }
144
145
        return $this->progressBar;
146
    }
147
148
    /**
149
     * @return ProgressBar
150
     */
151
    protected function getPB()
152
    {
153
        return $this->progressBar;
154
    }
155
156
    /**
157
     * @param array $options
158
     *
159
     * @return PHPoole
160
     */
161
    public function getPHPoole(array $options = [])
162
    {
163
        // debug mode?
164
        if (array_key_exists('debug', $options) && $options['debug']) {
165
            $this->debug = true;
166
        }
167
        // quiet mode?
168
        if (array_key_exists('quiet', $options) && $options['quiet']) {
169
            $this->quiet = true;
170
        }
171
172
        // CLI custom message callback function
173
        $messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
174
            switch ($code) {
175
                case 'LOCATE':
176
                case 'CREATE':
177
                case 'CONVERT':
178
                case 'GENERATE':
179
                case 'MENU':
180
                case 'COPY':
181
                case 'RENDER':
182
                    $this->wlAnnonce($message);
183
                    break;
184
                case 'TIME':
185
                    $this->wl($message);
186
                    break;
187
                case 'LOCATE_PROGRESS':
188
                case 'CREATE_PROGRESS':
189
                case 'CONVERT_PROGRESS':
190
                case 'GENERATE_PROGRESS':
191
                case 'MENU_PROGRESS':
192
                case 'COPY_PROGRESS':
193
                case 'RENDER_PROGRESS':
194
                    if ($this->debug) {
195
                        if ($itemsCount > 0) {
196
                            $this->wlDone(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message));
197
                            break;
198
                        }
199
                        $this->wlDone("$message");
200
                    } else {
201
                        if (!$this->quiet) {
202
                            if (isset($itemsCount) && $itemsMax > 0) {
203
                                $this->newPB(0, $itemsMax);
204
                                $this->getPB()->update($itemsCount, "$message");
205
                                if ($itemsCount == $itemsMax) {
206
                                    $this->getPB()->update($itemsCount, "[$itemsCount/$itemsMax]");
207
                                    $this->getPB()->finish();
208
                                }
209
                            } else {
210
                                $this->wl($message);
211
                            }
212
                        }
213
                    }
214
                    break;
215
                case 'LOCATE_ERROR':
216
                case 'CREATE_ERROR':
217
                case 'CONVERT_ERROR':
218
                case 'GENERATE_ERROR':
219
                case 'MENU_ERROR':
220
                case 'COPY_ERROR':
221
                case 'RENDER_ERROR':
222
                    $this->wlError($message);
223
                    break;
224
            }
225
        };
226
227
        // instanciate PHPoole?
228
        if (!$this->phpoole instanceof PHPoole) {
229
            if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) {
230
                throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath()));
231
            }
232
233
            try {
234
                $optionsFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE));
235
                if (is_array($options)) {
236
                    $options = array_replace_recursive($optionsFile, $options);
237
                }
238
                $this->phpoole = new PHPoole($options, $messageCallback);
239
                $this->phpoole->setSourceDir($this->getPath());
240
                $this->phpoole->setDestinationDir($this->getPath());
241
            } catch (ParseException $e) {
242
                throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage()));
243
            } catch (\Exception $e) {
244
                throw new \Exception(sprintf($e->getMessage()));
245
            }
246
        }
247
248
        return $this->phpoole;
249
    }
250
251
    /**
252
     * @param string $text
253
     */
254
    public function wl($text)
255
    {
256
        $this->console->writeLine($text);
257
    }
258
259
    /**
260
     * @param string $text
261
     */
262
    public function wlAnnonce($text)
263
    {
264
        $this->console->writeLine($text, Color::WHITE);
265
    }
266
267
    /**
268
     * @param string $text
269
     */
270
    public function wlDone($text)
271
    {
272
        $this->console->writeLine($text, Color::GREEN);
273
    }
274
275
    /**
276
     * @param string $text
277
     */
278
    public function wlAlert($text)
279
    {
280
        $this->console->writeLine($text, Color::YELLOW);
281
    }
282
283
    /**
284
     * @param string $text
285
     */
286
    public function wlError($text)
287
    {
288
        $this->console->writeLine($text, Color::RED);
289
    }
290
}
291