Passed
Push — master ( ff9e28...8752e2 )
by Francesc
01:52
created

ConsoleAbstract::setDevName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of FSConsoleTools
4
 * Copyright (C) 2018 Francesc Pineda Segarra <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScriptsUtils\Console;
21
22
use FacturaScripts\Core\Base\DataBase;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\DataBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
const DS = DIRECTORY_SEPARATOR;
25
26
if (!\defined('FS_FOLDER')) {
27
    \define('FS_FOLDER', __DIR__ . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS . '..' . DS);
28
}
29
30
if (!\defined('FS_DEBUG')) {
31
    \define('FS_DEBUG', true);
32
}
33
34
/**
35
 * Class ConsoleAbstract
36
 *
37
 * @author Francesc Pineda Segarra <[email protected]>
38
 */
39
abstract class ConsoleAbstract
40
{
41
    /**
42
     * Arguments received from command execution.
43
     *
44
     * @var array
45
     */
46
    protected $argv;
47
48
    /**
49
     * DataBase object.
50
     *
51
     * @var DataBase
52
     */
53
    protected $dataBase;
54
55
    /**
56
     * Developer name.
57
     *
58
     * @var string
59
     */
60
    private $devName;
61
62
    /**
63
     * Developer email.
64
     *
65
     * @var string
66
     */
67
    private $devMail;
68
69
    /**
70
     * Start point to run the command.
71
     *
72
     * @param array $params
73
     *
74
     * @return int
75
     */
76
    abstract public function run(...$params): int;
77
78
    /**
79
     * Return description about this class.
80
     *
81
     * @return string
82
     */
83
    abstract public function getDescription(): string;
84
85
    /**
86
     * Print help information to the user.
87
     *
88
     * @return string
89
     */
90
    abstract public function getHelpMsg(): string;
91
92
    /**
93
     * ConsoleAbstract constructor.
94
     */
95
    public function __construct()
96
    {
97
        $this->setDevName('YOUR NAME');
98
        $this->setDevMail('YOUR@EMAIL');
99
    }
100
    /**
101
     * Initialize.
102
     */
103
    public function init()
104
    {
105
        if (\DB_CONNECTION) {
106
            if (!isset($this->dataBase)) {
107
                $this->dataBase = new DataBase();
108
                $this->dataBase->connect();
109
            }
110
        } else {
111
            echo 'A database connection is needed. Do you set your config.php file?';
112
        }
113
    }
114
115
    /**
116
     * Terminate.
117
     */
118
    public function terminate()
119
    {
120
        $this->dataBase->close();
121
    }
122
123
    /**
124
     * Returns an associative array of available methods for the user.
125
     * Add more options if you want to add support for custom methods.
126
     *      [
127
     *          '-h'        => 'getHelpMsg',
128
     *          '--help'    => 'getHelpMsg',
129
     *      ]
130
     *
131
     * @return array
132
     */
133
    public function getUserMethods(): array
134
    {
135
        return [
136
            '-h' => 'getHelpMsg',
137
            '--help' => 'getHelpMsg',
138
        ];
139
    }
140
141
    /**
142
     * Ask user to continue and return a boolean.
143
     *
144
     * @param bool $autoReply
145
     *
146
     * @return bool
147
     */
148
    public function areYouSure($autoReply = false)
149
    {
150
        echo \PHP_EOL . 'Are you sure? [y/n] ';
151
        $stdin = fgets(STDIN);
152
        switch (trim($stdin)) {
153
            case 'y':
154
            case 'Y':
155
            case 'yes':
156
            case 'Yes':
157
                return true;
158
            default:
159
                return $autoReply;
160
        }
161
    }
162
163
    /**
164
     * Set database.
165
     *
166
     * @param DataBase $dataBase
167
     */
168
    public function setDataBase(DataBase $dataBase)
169
    {
170
        $this->dataBase = $dataBase;
171
    }
172
173
    /**
174
     * Return the DataBase object.
175
     *
176
     * @return DataBase
177
     */
178
    public function getDataBase()
179
    {
180
        return $this->dataBase;
181
    }
182
183
    /**
184
     * Returns developer name.
185
     *
186
     * @return string
187
     */
188
    public function getDevName(): string
189
    {
190
        return $this->devName;
191
    }
192
193
    /**
194
     * Sets developer name.
195
     *
196
     * @param string $devName
197
     */
198
    public function setDevName(string $devName)
199
    {
200
        $this->devName = $devName;
201
    }
202
203
    /**
204
     * Returns developer email.
205
     *
206
     * @return string
207
     */
208
    public function getDevMail(): string
209
    {
210
        return $this->devMail;
211
    }
212
213
    /**
214
     * Sets developer email
215
     *
216
     * @param string $devMail
217
     */
218
    public function setDevMail(string $devMail)
219
    {
220
        $this->devMail = $devMail;
221
    }
222
223
    /**
224
     * Save file.
225
     *
226
     * @param string $pathName
227
     * @param string $content
228
     *
229
     * @return bool|int
230
     */
231
    protected function saveFile(string $pathName, string $content)
232
    {
233
        return file_put_contents($pathName, $content);
234
    }
235
}
236