Completed
Push — Drafts ( 9b6bbd )
by Arnaud
04:41 queued 03:11
created

AbstractCommand::getRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Yaml\Yaml;
15
use Zend\Console\Adapter\AdapterInterface as Console;
16
use Zend\Console\ColorInterface as Color;
17
use ZF\Console\Route;
18
19
abstract class AbstractCommand
20
{
21
    const CONFIG_FILE = 'phpoole.yml';
22
23
    /**
24
     * @var Console
25
     */
26
    protected $console;
27
28
    /**
29
     * @var Route
30
     */
31
    protected $route;
32
33
    /**
34
     * @var string
35
     */
36
    protected $path;
37
38
    /**
39
     * @var PHPoole
40
     */
41
    protected $phpoole;
42
43
    /**
44
     * Start command processing.
45
     *
46
     * @param Route   $route
47
     * @param Console $console
48
     *
49
     * @return mixed
50
     */
51
    public function __invoke(Route $route, Console $console)
52
    {
53
        $this->route = $route;
54
        $this->console = $console;
55
56
        $this->path = realpath($this->route->getMatchedParam('path', getcwd()));
57
        if (!is_dir($this->path)) {
58
            $this->wlError('Invalid directory provided!');
59
            exit(2);
60
        }
61
        $this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path);
62
63
        return $this->processCommand();
64
    }
65
66
    /**
67
     * Process the command.
68
     */
69
    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...
70
71
    /**
72
     * @return Console
73
     */
74
    public function getConsole()
75
    {
76
        return $this->console;
77
    }
78
79
    /**
80
     * @return Route
81
     */
82
    public function getRoute()
83
    {
84
        return $this->route;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getPath()
91
    {
92
        return $this->path;
93
    }
94
95
    /**
96
     * @param array $options
97
     *
98
     * @return PHPoole
0 ignored issues
show
Documentation introduced by
Should the return type not be PHPoole|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
99
     */
100
    public function getPHPoole(array $options=[])
101
    {
102
        $messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0, $verbose = true) {
103
            switch (true) {
104
                case $code == 'CREATE'
105
                || $code == 'CONVERT'
106
                || $code == 'GENERATE'
107
                || $code == 'RENDER'
108
                || $code == 'COPY':
109
                    $this->wlAnnonce($message);
110
                    break;
111
                case $code == 'CREATE_PROGRESS'
112
                || $code == 'CONVERT_PROGRESS'
113
                || $code == 'GENERATE_PROGRESS'
114
                || $code == 'RENDER_PROGRESS'
115
                || $code == 'COPY_PROGRESS':
116
                    if ($itemsCount > 0 && $verbose !== false) {
117
                        $this->wlDone(sprintf("\r  (%u/%u) %s", $itemsCount, $itemsMax, $message));
118
                        break;
119
                    }
120
                    $this->wlDone("  $message");
121
                    break;
122
            }
123
        };
124
125
        if (!$this->phpoole instanceof PHPoole) {
126
            if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) {
127
                $this->wlError('Config file (phpoole.yml) not found!');
128
                exit(2);
129
            }
130
131
            try {
132
                $optionsFile = (new Yaml())->parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE));
133
                if (is_array($options)) {
134
                    $options = array_replace_recursive($optionsFile, $options);
135
                }
136
                $this->phpoole = new PHPoole($options, $messageCallback);
137
                $this->phpoole->setSourceDir($this->getPath());
138
                $this->phpoole->setDestinationDir($this->getPath());
139
            } catch (\Exception $e) {
140
                $this->wlError($e->getMessage());
141
                exit(2);
142
            }
143
        }
144
145
        return $this->phpoole;
146
    }
147
148
    /**
149
     * @param $text
150
     */
151
    public function wlAnnonce($text)
152
    {
153
        $this->console->writeLine($text, Color::WHITE);
154
    }
155
156
    /**
157
     * @param $text
158
     */
159
    public function wlDone($text)
160
    {
161
        $this->console->writeLine($text, Color::GREEN);
162
    }
163
164
    /**
165
     * @param $text
166
     */
167
    public function wlAlert($text)
168
    {
169
        $this->console->writeLine($text, Color::YELLOW);
170
    }
171
172
    /**
173
     * @param $text
174
     */
175
    public function wlError($text)
176
    {
177
        $this->console->writeLine($text, Color::RED);
178
    }
179
}
180