Completed
Push — build-options-enhance ( ca19e3 )
by Arnaud
05:39
created

AbstractCommand::messageCallback()   D

Complexity

Conditions 31
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 4.1666
c 0
b 0
f 0
cc 31
nc 1
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * Print progress bar.
158
     */
159
    protected function printPB($itemsCount, $itemsMax, $message)
160
    {
161
        $this->newPB(0, $itemsMax);
162
        $this->getPB()->update($itemsCount, "$message");
163
        if ($itemsCount == $itemsMax) {
164
            $this->getPB()->update($itemsCount, "[$itemsCount/$itemsMax]");
165
            $this->getPB()->finish();
166
        }
167
    }
168
169
    /**
170
     * @param array $config
171
     * @param array $options
172
     *
173
     * @return PHPoole
174
     */
175
    public function getPHPoole(
176
        array $config = ['debug' => false],
177
        array $options = ['verbosity' => PHPoole::VERBOSITY_NORMAL])
178
    {
179
        if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) {
180
            throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath()));
181
        }
182
183
        // debug mode?
184
        if ($options['debug']) {
185
            $this->debug = true;
186
        }
187
        // quiet mode?
188
        if ($options['verbosity'] == PHPoole::VERBOSITY_QUIET) {
189
            $this->quiet = true;
190
        }
191
192
        try {
193
            $configFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE));
194
            $config = array_replace_recursive($configFile, $config);
195
            $this->phpoole = (new PHPoole($config, $this->messageCallback()))
196
                ->setSourceDir($this->getPath())
197
                ->setDestinationDir($this->getPath());
198
        } catch (ParseException $e) {
199
            throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage()));
200
        } catch (\Exception $e) {
201
            throw new \Exception(sprintf($e->getMessage()));
202
        }
203
204
        return $this->phpoole;
205
    }
206
207
    /**
208
     * Custom message callback function
209
     */
210
    public function messageCallback()
211
    {
212
        return function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
213
            if ($this->quiet) {
214
                return;
215
            } else {
216
                switch ($code) {
217
                    case 'LOCATE':
218
                    case 'CREATE':
219
                    case 'CONVERT':
220
                    case 'GENERATE':
221
                    case 'MENU':
222
                    case 'COPY':
223
                    case 'RENDER':
224
                    case 'SAVE':
225
                        $this->wlAnnonce($message);
226
                        break;
227
                    case 'TIME':
228
                        $this->wl($message);
229
                        break;
230
                    case 'LOCATE_PROGRESS':
231
                    case 'CREATE_PROGRESS':
232
                    case 'CONVERT_PROGRESS':
233
                    case 'GENERATE_PROGRESS':
234
                    case 'MENU_PROGRESS':
235
                    case 'COPY_PROGRESS':
236
                    case 'RENDER_PROGRESS':
237
                    case 'SAVE_PROGRESS':
238
                        if ($this->debug) {
239
                            if ($itemsCount > 0) {
240
                                $this->wlDone(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message));
241
                                break;
242
                            }
243
                            $this->wlDone("$message");
244
                        } else {
245
                            if (isset($itemsCount) && $itemsMax > 0) {
246
                                printPB($itemsCount, $itemsMax, $message);
247
                            } else {
248
                                $this->wl($message);
249
                            }
250
                        }
251
                        break;
252
                    case 'LOCATE_ERROR':
253
                    case 'CREATE_ERROR':
254
                    case 'CONVERT_ERROR':
255
                    case 'GENERATE_ERROR':
256
                    case 'MENU_ERROR':
257
                    case 'COPY_ERROR':
258
                    case 'RENDER_ERROR':
259
                    case 'SAVE_ERROR':
260
                        $this->wlError($message);
261
                        break;
262
                }
263
            }
264
        };
265
    }
266
267
    /**
268
     * @param string $text
269
     */
270
    public function wl($text)
271
    {
272
        $this->console->writeLine($text);
273
    }
274
275
    /**
276
     * @param string $text
277
     */
278
    public function wlAnnonce($text)
279
    {
280
        $this->console->writeLine($text, Color::WHITE);
281
    }
282
283
    /**
284
     * @param string $text
285
     */
286
    public function wlDone($text)
287
    {
288
        $this->console->writeLine($text, Color::GREEN);
289
    }
290
291
    /**
292
     * @param string $text
293
     */
294
    public function wlAlert($text)
295
    {
296
        $this->console->writeLine($text, Color::YELLOW);
297
    }
298
299
    /**
300
     * @param string $text
301
     */
302
    public function wlError($text)
303
    {
304
        $this->console->writeLine($text, Color::RED);
305
    }
306
}
307