Completed
Push — master ( dde429...22637e )
by Andrii
20:00
created

DefaultGoal::prepareCommand()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4286
cc 3
eloc 8
nc 2
nop 1
crap 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A DefaultGoal::passthru() 0 4 1
A DefaultGoal::getRobo() 0 4 1
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) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\goals;
13
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * Default Goal.
19
 */
20
class DefaultGoal extends BaseGoal
21
{
22
    public $goalName;
23
24
    public $done = [];
25
26
    protected $_fileType = null;
27
28
    public function getFileType()
29
    {
30
        return $this->_fileType;
31
    }
32
33
    public function setFileType($type)
34
    {
35
        $this->_fileType = $type;
36
    }
37
38
    public function setDeps($deps)
39
    {
40
        $res = $this->getDeps();
41
        foreach (Helper::ksplit($deps) as $d => $e) {
42
            $res[is_int($d) ? $e : $d] = $e;
43
        }
44
        $this->setItem('deps', $res);
45
    }
46
47
    public function getDeps()
48
    {
49
        return Helper::ksplit($this->rawItem('deps'));
50
    }
51
52
    public function actionDeps()
53
    {
54
        foreach ($this->getDeps() as $name => $enabled) {
55
            if ($enabled) {
56
                $res = $this->module->runRequest($name);
57
                if (static::isNotOk($res)) {
58
                    return $res;
59
                }
60
            }
61
        }
62
63
        return 0;
64
    }
65
66
    public function runRequest($request)
67
    {
68
        return $request !== null ? $this->module->runRequest($request) : null;
69
    }
70
71
    /**
72
     * Runs array of requests. Stops on failure and returns exit code.
73
     * @param null|string|array $requests
74
     */
75
    public function runRequests($requests)
76
    {
77
        if (is_string($requests)) {
78
            $requests = [$requests];
79
        } elseif (!is_array($requests)) {
80
            return 0;
81
        }
82
        foreach ($requests as $request) {
83
            $res = $this->runRequest($request);
84
            if (static::isNotOk($res)) {
85
                return $res;
86
            }
87
        }
88
89
        return 0;
90
    }
91
92
    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...
93
    {
94
        if ($this->done[$action]) {
95
            Yii::trace("Already done: $this->goalName/$action");
96
97
            return true;
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * Mark action as already done.
105
     *
106
     * @param string $action action id
107
     * @param int $time microtime when action was done, false for action was not done
108
     */
109
    public function markDone($action, $time = null)
110
    {
111
        $this->done[$action] = ($time === null || $time === true) ? microtime(1) : $time;
112
    }
113
114
    public function actionPerform($name = null, $path = null)
115
    {
116
        Yii::trace("Started: $this->goalName");
117
        return $this->runActions('deps, make');
118
    }
119
120
    public function actionLoad()
121
    {
122
        Yii::trace("Loading nothing for '$this->goalName'");
123
    }
124
125
    public function actionSave()
126
    {
127
        Yii::trace("Saving nothing for '$this->goalName'");
128
    }
129
130
    public function actionMake()
131
    {
132
        return $this->runActions('load, save');
133
    }
134
135
    public function runAction($id, $params = [])
136
    {
137
        if ($this->isDone($id)) {
138
            return;
139
        }
140
        $result = parent::runAction($id, $params);
141
        $this->markDone($id);
142
143
        return $result;
144
    }
145
146
    public function runActions($actions)
147
    {
148
        foreach (Helper::ksplit($actions) as $action) {
149
            $res = $this->runAction($action);
150
            if (static::isNotOk($res)) {
151
                return $res;
152
            }
153
        }
154
155
        return 0;
156
    }
157
158
    public static function isNotOk($res)
159
    {
160
        return is_object($res) ? $res->exitStatus : $res;
161
    }
162
163
    public function options($actionId)
164
    {
165
        return array_merge(parent::options($actionId), array_keys(Helper::getPublicVars(get_called_class())));
166
    }
167
168
    /**
169
     * Runs given binary with given arguments.
170
     * @param string $name
171
     * @param string $args
172
     * @return int exit code
173
     */
174
    public function passthru($name, $args = '')
175
    {
176
        return Yii::$app->get('binaries')->passthru($name, $args);
177
    }
178
179
    public function getRobo()
180
    {
181
        return Yii::$app->get('robo');
182
    }
183
184
    public function getConfig()
185
    {
186
        return Yii::$app->get('config');
187
    }
188
189
    public function getPackage()
190
    {
191
        return $this->getConfig()->get('package');
192
    }
193
194
    public function getVendor()
195
    {
196
        return $this->getConfig()->get('vendor');
197
    }
198
199
    public function getVcs()
200
    {
201
        /// TODO determine VCS
202
        return $this->getConfig()->get('git');
203
    }
204
}
205