Completed
Push — master ( 8cf48b...afece8 )
by Kris
13s queued 10s
created

CheckTrait   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 65
c 2
b 0
f 0
dl 0
loc 177
rs 9.84
wmc 32

6 Methods

Rating   Name   Duplication   Size   Complexity  
A printCheckScore() 0 5 2
A getLastReportsCategories() 0 10 3
B printCheckDetail() 0 29 7
A printLastReport() 0 19 6
B printCheckLastReports() 0 21 7
B printCheckReports() 0 18 7
1
<?php declare(strict_types=1);
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
 * Trait Check
27
 * 
28
 */
29
trait CheckTrait
30
{
31
    /**
32
     * Print confidence score 
33
     * 
34
     * @access protected
35
     * @static
36
     * @param object    $response
37
     * 
38
     * @return void
39
     */
40
    protected static function printCheckScore(object $response): void
41
    {
42
        $score = empty($response->data->abuseConfidenceScore) ? 0 : $response->data->abuseConfidenceScore;
43
        $line = Console::text(Console::pad('   Confidence score:', 23), 'white') . self::getScoreBadge($score);
44
        Console::log($line);
45
    }
46
47
    /**
48
     * Print check IP detail 
49
     * 
50
     * @access protected
51
     * @static
52
     * @param object    $response
53
     * @param string    $color
54
     * 
55
     * @return void
56
     */
57
    protected static function printCheckDetail(object $response, string $color): void
58
    {
59
        //      self::printResult('   isPublic', $response->data->isPublic, $defaultColor);
60
        //      self::printResult('   ipVersion', $response->data->ipVersion, $defaultColor);
61
 
62
        $line = self::printResult(Console::pad('   Whitelisted:', 23), $response->data->isWhitelisted ? 'true': 'false', $color, '', false);
63
        $line .= $response->data->isWhitelisted ? Console::text(' ★', 'green') : ''; 
64
        Console::log($line);
65
       
66
        self::printResult(Console::pad('   Country code:', 23), $response->data->countryCode, $color);
67
        
68
        if (!empty($response->data->countryName)){
69
            self::printResult(Console::pad('   Country name:', 23), $response->data->countryName, $color);
70
        }
71
72
        self::printResult(Console::pad('   ISP:', 23), $response->data->isp, $color);
73
74
        if ($response->data->usageType){
75
            $line = self::printResult(Console::pad('   Usage type:', 23), $response->data->usageType, $color, '', false);
76
            $line .= $response->data->usageType === 'Reserved' ? Console::text(' ◆', 'green') : '';
77
            Console::log($line);
78
        }
79
80
        $hostames = implode(', ', array_filter($response->data->hostnames)) ?? null;
81
        if (!empty($hostames)){
82
            self::printResult(Console::pad('   Hostname(s):', 23), $hostames, $color);
83
        }
84
85
        self::printResult(Console::pad('   Domain:', 23), $response->data->domain, $color);
86
    }
87
88
    /**
89
     * Print reports data 
90
     * 
91
     * @access protected
92
     * @static
93
     * @param object    $response
94
     * @param int       $maxAge
95
     * @param string    $color
96
     * 
97
     * @return void
98
     */
99
    protected static function printCheckReports(object $response, int $maxAge, string $color): void
100
    {
101
        $nbReport = $response->data->totalReports && is_numeric($response->data->totalReports) ? intval($response->data->totalReports) : 0;
102
        
103
        if ($nbReport > 0 ){
104
            $line  = self::printResult(Console::pad('   Total reports:', 23), $nbReport, $color, '', false);
105
            $line .= self::printResult(' from ', $response->data->numDistinctUsers, $color, '', false);
106
            $line .= Console::text($nbReport > 0 ? ' distinct users': ' user', 'white');
107
            Console::log($line);
108
109
        } else {
110
            // no reports
111
            $day = $maxAge > 1 ? 'in last '. $maxAge . ' days': ' today';
112
            Console::log( Console::text('   ✓', 'green') . Console::text(' Not reported ' . $day));
113
        }
114
        
115
        if (!empty($response->data->lastReportedAt)){
116
            self::printResult(Console::pad('   Last reported at:', 23), self::getDate($response->data->lastReportedAt), $color);
117
        }
118
    }
119
    
120
    /**
121
     * Print last reports data 
122
     * 
123
     * @access protected
124
     * @static
125
     * @param object    $response
126
     * @param bool      $verbose
127
     * @param int       $maxReportsNumber
128
     * 
129
     * @return void
130
     */
131
    protected static function printCheckLastReports(object $response, int $maxReportsNumber): void
132
    {
133
        $nbLastReports = isset($response->data->reports) ? count($response->data->reports) : 0;
134
            
135
        if ($nbLastReports > 0){
136
            Console::log('   Last reports:', 'white');
137
            $numberDiplayedReports = 0;
138
            $defaultColor = 'lightyellow'; // reset color for last reports
139
        
140
            foreach ($response->data->reports as $report){
141
                self::printLastReport($report);
142
                $numberDiplayedReports++;
143
144
                if ($numberDiplayedReports === $maxReportsNumber || $numberDiplayedReports === $nbLastReports) {
145
                    $line  = Console::text('      (', 'white');
146
                    $line .= Console::text($numberDiplayedReports, $defaultColor);
147
                    $line .= Console::text('/', 'white');
148
                    $line .= Console::text($nbLastReports, $defaultColor);
149
                    $line .= Console::text($numberDiplayedReports > 1 ? ' reports displayed)': ' report displayed)', 'white');
150
                    Console::log($line);
151
                    break;
152
                }
153
            }            
154
        }
155
    }
156
    
157
    /**
158
     * Print single entry in last reports 
159
     * 
160
     * @access private
161
     * @static
162
     * @param object    $report
163
     * 
164
     * @return void
165
     */
166
    private static function printLastReport(object $report): void
167
    {
168
        $categories = self::getLastReportsCategories($report);
169
        $defaultColor = 'lightyellow'; // reset color for last reports
170
                            
171
        $line  = Console::text('    →', $defaultColor);
172
        $line .= self::printResult(' reported at: ', self::getDate($report->reportedAt), $defaultColor, '', false);
173
  //    $line .= self::printResult(' by user: ', $report->reporterId, $defaultColor, '', false);
174
        if (isset($report->reporterCountryCode) && isset($report->reporterCountryName)){
175
            $line .= Console::text(' from: ', 'white');
176
            $line .= self::printResult('', $report->reporterCountryCode, $defaultColor, '', false);
177
            $line .= Console::text(' - ', 'white');
178
            $line .= self::printResult('', $report->reporterCountryName, $defaultColor, '', false);
179
        }
180
        $line .= Console::text(' with categor' .  (count($categories) > 1 ? "ies: " : "y: "), 'white');
181
        foreach ($categories as $key => $cat) {
182
            $line .= Console::text($key==0 ? '' : ',' , 'white') . Console::text($cat, $defaultColor);
183
        }
184
        Console::log($line);
185
    }  
186
187
    /**
188
     * Get last report categories array 
189
     * 
190
     * @access private
191
     * @static
192
     * @param object    $report
193
     * 
194
     * @return array
195
     */
196
    private static function getLastReportsCategories(object $report): array
197
    {
198
        $categories = [];
199
        foreach (array_filter($report->categories) as $catId){
200
            $cat = ApiHandler::getCategoryNamebyId($catId)[0];
201
            if ($cat !== false) {
202
                $categories[] = $cat;
203
            }
204
        }
205
        return $categories;                          
206
   }    
207
}