Passed
Branch dev (37def1)
by Kris
01:40
created

CheckTrait   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

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

6 Methods

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