Completed
Pull Request — build-options (#162)
by Arnaud
09:45 queued 07:00
created

AbstractCommand   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 8
dl 0
loc 258
rs 9.76
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 22 4
processCommand() 0 1 ?
A getConsole() 0 4 1
A getRoute() 0 4 1
A getPath() 0 4 1
A createProgressBar() 0 17 3
A getProgressBar() 0 4 1
A printProgressBar() 0 9 2
B getPHPoole() 0 30 6
B messageCallback() 0 31 9
A wl() 0 4 1
A wlAnnonce() 0 4 1
A wlDone() 0 4 1
A wlAlert() 0 4 1
A wlError() 0 4 1
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 $progressBarMax = 0;
55
    /**
56
     * @var bool
57
     */
58
    protected $verbose = 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 createProgressBar($start, $max)
131
    {
132
        if ($this->progressBar === null || $max != $this->progressBarMax) {
133
            $this->progressBarMax = $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 getProgressBar()
152
    {
153
        return $this->progressBar;
154
    }
155
156
    /**
157
     * Print progress bar.
158
     */
159
    protected function printProgressBar($itemsCount, $itemsMax, $message)
160
    {
161
        $this->createProgressBar(0, $itemsMax);
162
        $this->getProgressBar()->update($itemsCount, "$message");
163
        if ($itemsCount == $itemsMax) {
164
            $this->getProgressBar()->update($itemsCount, "[$itemsCount/$itemsMax]");
165
            $this->getProgressBar()->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
        // verbosity: verbose
183
        if ($options['verbosity'] == PHPoole::VERBOSITY_VERBOSE) {
184
            $this->verbose = true;
185
        }
186
        // verbosity: quiet
187
        if ($options['verbosity'] == PHPoole::VERBOSITY_QUIET) {
188
            $this->quiet = true;
189
        }
190
191
        try {
192
            $configFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE));
193
            $config = array_replace_recursive($configFile, $config);
194
            $this->phpoole = (new PHPoole($config, $this->messageCallback()))
195
                ->setSourceDir($this->getPath())
196
                ->setDestinationDir($this->getPath());
197
        } catch (ParseException $e) {
198
            throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage()));
199
        } catch (\Exception $e) {
200
            throw new \Exception(sprintf($e->getMessage()));
201
        }
202
203
        return $this->phpoole;
204
    }
205
206
    /**
207
     * Custom message callback function.
208
     */
209
    public function messageCallback()
210
    {
211
        return function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
212
            if ($this->quiet) {
213
                return;
214
            } else {
215
                if (strpos($code, '_PROGRESS') !== false) {
216
                    if ($this->verbose) {
217
                        if ($itemsCount > 0) {
218
                            $this->wlDone(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message));
219
220
                            return;
221
                        }
222
                        $this->wlDone("$message");
223
                    } else {
224
                        if (isset($itemsCount) && $itemsMax > 0) {
225
                            $this->printProgressBar($itemsCount, $itemsMax, $message);
226
                        } else {
227
                            $this->wl($message);
228
                        }
229
                    }
230
                } elseif (strpos($code, '_ERROR') !== false) {
231
                    $this->wlError($message);
232
                } elseif ($code == 'TIME') {
233
                    $this->wl($message);
234
                } else {
235
                    $this->wlAnnonce($message);
236
                }
237
            }
238
        };
239
    }
240
241
    /**
242
     * @param string $text
243
     */
244
    public function wl($text)
245
    {
246
        $this->console->writeLine($text);
247
    }
248
249
    /**
250
     * @param string $text
251
     */
252
    public function wlAnnonce($text)
253
    {
254
        $this->console->writeLine($text, Color::WHITE);
255
    }
256
257
    /**
258
     * @param string $text
259
     */
260
    public function wlDone($text)
261
    {
262
        $this->console->writeLine($text, Color::GREEN);
263
    }
264
265
    /**
266
     * @param string $text
267
     */
268
    public function wlAlert($text)
269
    {
270
        $this->console->writeLine($text, Color::YELLOW);
271
    }
272
273
    /**
274
     * @param string $text
275
     */
276
    public function wlError($text)
277
    {
278
        $this->console->writeLine($text, Color::RED);
279
    }
280
}
281