ShellUtils   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 232
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 69
c 5
b 0
f 0
dl 0
loc 232
rs 10
wmc 22

10 Methods

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