Completed
Push — master ( 9039c2...e30981 )
by Andrii
03:01
created

AbstractController::readpassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\controllers;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * Abstract controller.
19
 */
20
abstract class AbstractController extends \hidev\base\Controller
21
{
22
    protected $_before = [];
23
    protected $_after  = [];
24
    protected $_make   = ['load', 'save'];
25
26
    /**
27
     * @var array list of performed actions
28
     */
29
    protected $_done = [];
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function options($actionId)
35
    {
36
        return array_merge(parent::options($actionId), array_keys(Helper::getPublicVars(get_called_class())));
37
    }
38
39
    public function perform()
40
    {
41
        return $this->runActions(['before', 'make', 'after']);
42
    }
43
44
    public function actionBefore()
45
    {
46
        return $this->runRequests($this->getBefore());
47
    }
48
49
    public function actionMake()
50
    {
51
        return $this->runActions($this->getMake());
52
    }
53
54
    public function actionAfter()
55
    {
56
        return $this->runRequests($this->getAfter());
57
    }
58
59
    public function actionLoad()
60
    {
61
        Yii::trace("Loading nothing for '$this->id'");
62
    }
63
64
    public function actionSave()
65
    {
66
        Yii::trace("Saving nothing for '$this->id'");
67
    }
68
69
    public function setBefore($requests)
70
    {
71
        $this->_before = array_merge($this->getBefore(), $this->normalizeTasks($requests));
72
    }
73
74
    public function getBefore()
75
    {
76
        return $this->_before;
77
    }
78
79
    public function setMake($requests)
80
    {
81
        $this->_make = array_merge($this->getMake(), $this->normalizeTasks($requests));
82
    }
83
84
    public function getMake()
85
    {
86
        return $this->_make;
87
    }
88
89
    public function setAfter($requests)
90
    {
91
        $this->_after = array_merge($this->getAfter(), $this->normalizeTasks($requests));
92
    }
93
94
    public function getAfter()
95
    {
96
        return $this->_after;
97
    }
98
99
    public function normalizeTasks($tasks)
100
    {
101
        if (!$tasks) {
102
            return [];
103
        } elseif (!is_array($tasks)) {
104
            $tasks = [(string) $tasks => 1];
105
        }
106
        $res = [];
107
        foreach ($tasks as $dep => $enabled) {
108
            $res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled);
109
        }
110
111
        return $res;
112
    }
113
114
    /**
115
     * Runs array of requests. Stops on failure and returns exit code.
116
     * @param null|string|array $requests
117
     * @return int|Response exit code
118
     */
119 View Code Duplication
    public function runRequests($requests)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        foreach ($this->normalizeTasks($requests) as $request => $enabled) {
122
            if ($enabled) {
123
                $res = $this->runRequest($request);
124
                if (static::isNotOk($res)) {
125
                    return $res;
126
                }
127
            }
128
        }
129
130
        return 0;
131
    }
132
133
    public function runRequest($request)
134
    {
135
        return $request === null ? null : $this->module->runRequest($request);
136
    }
137
138
    public static function isNotOk($res)
139
    {
140
        return is_object($res) ? $res->exitStatus : $res;
141
    }
142
143
    /**
144
     * Runs list of actions.
145
     * TODO: think to redo with runRequests.
146
     * @param null|string|array $actions
147
     * @return int|Response exit code
148
     */
149 View Code Duplication
    public function runActions($actions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        foreach ($this->normalizeTasks($actions) as $action => $enabled) {
152
            if ($enabled) {
153
                $res = $this->runAction($action);
154
                if (static::isNotOk($res)) {
155
                    return $res;
156
                }
157
            }
158
        }
159
160
        return 0;
161
    }
162
163
    public function runAction($id, $params = [])
164
    {
165
        if ($this->isDone($id)) {
166
            return;
167
        }
168
        $result = parent::runAction($id, $params);
169
        $this->markDone($id);
170
171
        return $result;
172
    }
173
174
    public function isDone($action, $timestamp = null)
0 ignored issues
show
Unused Code introduced by
The parameter $timestamp is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
175
    {
176
        if ($this->_done[$action]) {
177
            Yii::trace("Already done: '$this->id/$action'");
178
179
            return true;
180
        }
181
182
        return false;
183
    }
184
185
    /**
186
     * Mark action as already done.
187
     *
188
     * @param string $action action id
189
     * @param int $time microtime when action was done, false for action was not done
190
     */
191
    public function markDone($action, $time = null)
192
    {
193
        $this->_done[$action] = ($time === null || $time === true) ? microtime(1) : $time;
194
    }
195
196
    /**
197
     * Runs given binary with given arguments. Returns exit code.
198
     * @param string $name
199
     * @param string $args
200
     * @return int exit code
201
     */
202
    public function passthru($name, $args = '')
203
    {
204
        return $this->takeGoal('binaries')->passthru($name, $args);
205
    }
206
207
    /**
208
     * Runs given binary with given arguments. Returns stdout array.
209
     * @param string $name
210
     * @param string $args
211
     * @param bool $returnExitCode, default false
0 ignored issues
show
Documentation introduced by
There is no parameter named $returnExitCode,. Did you maybe mean $returnExitCode?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
212
     * @return array|int stdout or exitcode
213
     */
214
    public function exec($name, $args = '', $returnExitCode = false)
215
    {
216
        return $this->takeGoal('binaries')->exec($name, $args, $returnExitCode);
217
    }
218
219
    public function readline($prompt)
220
    {
221
        return readline($prompt);
222
    }
223
224
    public function readpassword($prompt)
225
    {
226
        echo $prompt;
227
        system('stty -echo');
228
        $password = rtrim(fgets(STDIN), PHP_EOL);
229
        system('stty echo');
230
        echo "\n";
231
        return $password;
232
    }
233
234
    public function takeGoal($id)
235
    {
236
        return Yii::$app->get('config')->getGoal($id);
237
    }
238
239
    public function takeConfig()
240
    {
241
        return Yii::$app->get('config');
242
    }
243
244
    public function takeVendor()
245
    {
246
        return $this->takeGoal('vendor');
247
    }
248
249
    public function takePackage()
250
    {
251
        return $this->takeGoal('package');
252
    }
253
254
    public function takeVcs()
255
    {
256
        return $this->takeConfig()->getVcs();
257
    }
258
}
259