Passed
Branch dev (044aa2)
by Kris
01:43
created

ShellUtils::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 *     _    _                    ___ ____  ____  ____
5
 *    / \  | |__  _   _ ___  ___|_ _|  _ \|  _ \| __ )
6
 *   / _ \ | '_ \| | | / __|/ _ \| || |_) | | | |  _ \
7
 *  / ___ \| |_) | |_| \__ \  __/| ||  __/| |_| | |_) |
8
 * /_/   \_\_.__/ \__,_|___/\___|___|_|   |____/|____/
9
 *
10
 * This file is part of Kristuff\AbsuseIPDB.
11
 *
12
 * (c) Kristuff <[email protected]>
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @version    0.9.10
18
 * @copyright  2020-2021 Kristuff
19
 */
20
namespace Kristuff\AbuseIPDB;
21
22
use Kristuff\Mishell\Console;
23
use Kristuff\AbuseIPDB\ApiHandler;
24
25
/**
26
 * Class ShellUtils
27
 * 
28
 * Abstract base class for main cli program
29
 */
30
abstract class ShellUtils
31
{
32
    /**
33
     * helper functions
34
     */
35
    use UtilsTrait;
36
  
37
    const OUTPUT_JSON       = 'json';
38
    const OUTPUT_DEFAULT    = 'default';
39
    const OUTPUT_PLAINTEXT  = 'plaintext';
40
    
41
    /**
42
     * @var string      $outputFormat
43
     */
44
    protected static $outputFormat = self::OUTPUT_DEFAULT; 
45
46
    /**
47
     * 
48
     */
49
    protected static function isDefaultOuput()
50
    {
51
        return self::$outputFormat === self::OUTPUT_DEFAULT; 
52
    }
53
54
    /**
55
     * Prints title action banner 
56
     * 
57
     * @access protected
58
     * @static
59
     * @param string    $title
60
     * 
61
     * @return void
62
     */
63
    protected static function printTitle(string $title)
64
    {
65
        if (self::isDefaultOuput()) {
66
            Console::log();
67
            Console::log($title);
68
            Console::log();
69
        }
70
    }
71
  
72
    /**
73
     * Print temp message during api request 
74
     * 
75
     * @access protected
76
     * @static
77
     * @param array $arguments
78
     * 
79
     * @return void
80
     */
81
    protected static function printTempMessage()
82
    {
83
        if (self::isDefaultOuput()) {
84
            Console::reLog(Console::text('   ? ', 'green') . Console::text('waiting for api response', 'white') . Console::text(' ... ', 'green'));
85
        }
86
    }
87
88
    /**
89
     * Clear the temp message set during api request 
90
     * 
91
     * @access protected
92
     * @static
93
     * @param array $arguments
94
     * 
95
     * @return void
96
     */
97
    protected static function clearTempMessage()
98
    {
99
        if (self::isDefaultOuput()) {
100
            // long blank string to overwrite previous message
101
            Console::reLog('                                                     ');
102
        }
103
    }
104
105
    /**
106
     * Print to banner 
107
     * 
108
     * @access protected
109
     * @static
110
     * @param array $arguments
111
     * 
112
     * @return void
113
     */
114
    protected static function printLogo()
115
    {
116
        if (self::isDefaultOuput()) {
117
            //Console::log("   _       _    _         __  __                   ", "darkgray");
118
            //Console::log("  | |___ _(_)__| |_ _  _ / _|/ _|                  ", "darkgray");
119
            //Console::log("  | / / '_| (_-<  _| || |  _|  _|                  ", "darkgray");
120
            //Console::log("  |_\_\_| |_/__/\__|\_,_|_| |_|                    ", "darkgray");
121
            Console::log("        _                 ___ ___ ___  ___        ", "darkgray");
122
            Console::log("   __ _| |__ _  _ ___ ___|_ _| _ \   \| _ )       ", "darkgray");
123
            Console::log("  / _` | '_ \ || (_-</ -_)| ||  _/ |) | _ \       ", "darkgray");
124
            Console::log("  \__,_|_.__/\_,_/__/\___|___|_| |___/|___/       ", "darkgray");
125
        }
126
    }
127
128
    /**
129
     * Print version 
130
     * 
131
     * @access protected
132
     * @static
133
     * @param array $arguments
134
     * 
135
     * @return void
136
     */
137
    protected static function printVersion()
138
    {
139
        self::printLogo();
140
141
        Console::log();
142
        Console::log(Console::text('  Kristuff/AbuseIPDB Client version: ', 'darkgray') . Console::text(AbuseIPDBClient::VERSION, 'lightgray'));
143
        Console::log(Console::text('  Kristuff/AbuseIPDB Core version:   ', 'darkgray') . Console::text(ApiHandler::VERSION, 'lightgray')); 
144
        Console::log(Console::text('  --------------------------------------------------', 'darkgray'));    
145
        Console::log(Console::text('  Released under the MIT licence', 'darkgray'));
146
        Console::log(Console::text('  Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray'));
147
        Console::log(
148
            Console::text('  © 2020-2021 Kristuff (', 'darkgray').
149
            Console::text('https://github.com/kristuff', 'darkgray', 'underlined').
150
            Console::text(')', 'darkgray')
151
        );
152
        Console::log(Console::text('  --------------------------------------------------', 'darkgray'));    
153
        Console::log();
154
    }
155
156
    /**
157
     * Print app banner
158
     * 
159
     * @access protected
160
     * @static
161
     * 
162
     * @return void
163
     */
164
    protected static function printBanner()
165
    {
166
        if (self::isDefaultOuput()) {
167
            Console::log();    
168
            Console::log( Console::text(' Kristuff\AbuseIPDB ', 'darkgray') . Console::text(' ' . AbuseIPDBClient::VERSION . ' ', 'white', 'blue')); 
169
            Console::log(Console::text(' Made with ', 'darkgray') . Console::text('♥', 'red') . Console::text(' in France', 'darkgray')); 
170
            Console::log(' © 2020-2021 Kristuff', 'darkgray'); 
171
            Console::log();  
172
        }  
173
    }
174
175
    /**
176
     * Print footer
177
     * 
178
     * @access protected
179
     * @static
180
     * 
181
     * @return void
182
     */
183
    protected static function printFooter(string $requestTime = '')
184
    {
185
        if (self::isDefaultOuput()) {
186
            if (!empty($requestTime)){
187
                $date_utc = new \DateTime("now", new \DateTimeZone("UTC"));
188
                Console::log(
189
                    Console::text('  Request time: ', 'darkgray') . Console::text($requestTime . 's', 'lightgray'). 
190
                    Console::text(' | UTC time: ', 'darkgray') . Console::text($date_utc->format('Y-m-d H:i:s'), 'lightgray')
191
                );
192
            }
193
            Console::log(Console::text('  ------------------------------------------------------------------------------------------------------', 'darkgray')); 
194
            Console::log(
195
                Console::text('  Kristuff\AbuseIPDB ', 'darkgray') . 
196
                Console::text(AbuseIPDBClient::VERSION, 'lightgray') . 
197
                Console::text(' | Made with ', 'darkgray') . 
198
                Console::text('♥', 'red') .
199
                Console::text(' in France | © 2020-2021 Kristuff (https://github.com/kristuff)', 'darkgray')
200
            ); 
201
            Console::log(); 
202
        }   
203
    }
204
205
    /**
206
     * Prints/gets a result value 
207
     * 
208
     * @access protected
209
     * @static
210
     * 
211
     * @return string
212
     */
213
    protected static function printResult($text, $value, string $foregroundColor = 'lightred', string $backgroundColor = '', bool $print = true)
214
    {
215
        
216
        // do not print null/blank values
217
        if (isset($value)){
218
            $line = Console::text($text, 'white') . Console::text($value, $foregroundColor, $backgroundColor); 
219
            if ($print && self::isDefaultOuput()){
220
                Console::log($line);
221
            }
222
            return $line;
223
        }
224
        return '';
225
    }
226
 
227
    /**
228
     * Prints score badge 
229
     * 
230
     * @access protected
231
     * @static
232
     * @param string    $text       
233
     * @param int       $score     
234
     * @param string    $textColor
235
     * 
236
     * @return string
237
     */
238
    protected static function getScoreBadge(int $score, string $padding = ' ')
239
    {
240
        $scoreforegroundColor = 'white';
241
        $scoreBackgroundColor = 'green';
242
243
        if (intval($score) > 0 ){
244
            $scoreforegroundColor = 'black';
245
            $scoreBackgroundColor = 'yellow';
246
        } 
247
        if (intval($score) > 50 ){
248
            $scoreforegroundColor = 'white';
249
            $scoreBackgroundColor = 'red';
250
        } 
251
  
252
        $badge = str_pad($score, 3, ' ',STR_PAD_LEFT); 
253
        return Console::text($padding.$badge.$padding, $scoreforegroundColor, $scoreBackgroundColor);
254
    }
255
}