Passed
Branch dev (c721e8)
by Kris
02:10
created

CheckBlockTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B printCheckBlockReportedIP() 0 39 9
A printCheckBlockDetail() 0 8 1
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
24
/**
25
 * Trait Check
26
 * 
27
 */
28
trait CheckBlockTrait
29
{
30
    /**
31
     * Print reported IP 
32
     * 
33
     * @access protected
34
     * @static
35
     * @param object    $response
36
     * 
37
     * @return void
38
     */
39
    protected static function printCheckBlockDetail(object $response)
40
    {
41
        self::printResult(Console::pad('   Network Address:', 23), $response->data->networkAddress, 'lightyellow');
42
        self::printResult(Console::pad('   Netmask:', 23), $response->data->netmask, 'lightyellow');
43
        self::printResult(Console::pad('   Min Address:', 23), $response->data->minAddress, 'lightyellow');
44
        self::printResult(Console::pad('   Max Address:', 23), $response->data->maxAddress, 'lightyellow');
45
        self::printResult(Console::pad('   Possible Hosts:', 23), $response->data->numPossibleHosts, 'lightyellow');
46
        self::printResult(Console::pad('   Address SpaceDesc:', 23), $response->data->addressSpaceDesc, 'lightyellow');
47
    }   
48
    
49
    /**
50
     * Print reported IP 
51
     * 
52
     * @access protected
53
     * @static
54
     * @param object    $response
55
     * @param int       $maxAge
56
     * @param int       $limit
57
     * 
58
     * @return void
59
     */
60
    protected static function printCheckBlockReportedIP(object $response, int $maxAge, int $limit)
61
    {
62
        $nbReports = isset($response->data->reportedAddress) ? count($response->data->reportedAddress) : 0;
63
        
64
        if ($nbReports > 0) {
65
            self::printResult(Console::pad('   Reported addresses:', 23), $nbReports, 'lightyellow');
66
            $numberDiplayedReports = 0;
67
               
68
            foreach ($response->data->reportedAddress as $report){
69
                $score = empty($report->abuseConfidenceScore) ? 0 : $report->abuseConfidenceScore;
70
                $defaultColor = self::getScoreColor($score); // color based on score
71
      
72
                $line  = Console::text('   →', $defaultColor);
73
                $line .= self::printResult(' IP: ', $report->ipAddress, $defaultColor, '', false);
74
                $line .= self::printResult(' Country: ', $report->countryCode , $defaultColor, '', false);
75
                $line .= Console::text(' | Confidence score: ', 'white');
76
                $line .= self::getScoreBadge($score);
77
                $line .= self::printResult(' Total reports: ', $report->numReports, $defaultColor, '', false);
78
                $line .= self::printResult(' Last reported at: ', self::getDate($report->mostRecentReport), $defaultColor, '', false);
79
                Console::log($line);
80
      
81
                // counter
82
                $numberDiplayedReports++;
83
      
84
                if ($numberDiplayedReports === $limit || $numberDiplayedReports === $nbReports) {
85
                    $line  = Console::text('      (', 'white');
86
                    $line .= Console::text($numberDiplayedReports, 'lightyellow');
87
                    $line .= Console::text('/', 'white');
88
                    $line .= Console::text($nbReports, 'lightyellow');
89
                    $line .= Console::text($numberDiplayedReports > 1 ? ' IPs displayed)': ' IP displayed)', 'white');
90
                    Console::log($line);
91
                    break;
92
                }
93
            }
94
      
95
        } else {
96
            // no reports
97
            $day = $maxAge > 1 ? 'in last '. $maxAge . ' days': ' today';
98
            Console::log( Console::text('    ✓', 'green') . Console::text(' No IP reported ' . $day));
99
        }
100
    }   
101
}