Passed
Push — master ( fa42da...3c5c71 )
by Kris
01:35
created

SilentApiHandler::blacklist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 3
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.9
18
 * @copyright  2020-2021 Kristuff
19
 */
20
21
namespace Kristuff\AbuseIPDB;
22
23
/**
24
 * Class SilentApiHandler
25
 * 
26
 * Overwrite ApiHandler with Exception handling
27
 * Instead of Exception, all method return an ApiResponse that may 
28
 * contains errors from the AbuseIPDB API, or internal errors  
29
 */
30
class SilentApiHandler extends ApiHandler
31
{
32
    /**
33
     * Get an internal error message in an ApiResponse object
34
     * 
35
     * @access public
36
     * @param string    $message        The error message
37
     *
38
     * @return ApiResponse
39
     */
40
    public function getErrorResponse(string $message): ApiResponse
41
    {
42
        $response = [
43
            "errors" => [
44
                [
45
                    "title"  => "Internal Error",
46
                    "detail" => $message
47
                ]
48
            ]
49
        ];
50
51
        return new ApiResponse(json_encode($response));
52
    }
53
54
    /**
55
     * Performs a 'report' api request, with Exception handling
56
     * 
57
     * @access public
58
     * @param string    $ip             The ip to report
59
     * @param string    $categories     The report category(es)
60
     * @param string    $message        The report message
61
     *
62
     * @return ApiResponse
63
     */
64
    public function report(string $ip, string $categories, string $message): ApiResponse
65
    {
66
        try {
67
            return parent::report($ip,$categories,$message);
68
        } catch (\Exception $e) {
69
            return $this->getErrorResponse($e->getMessage());
70
        }
71
    }
72
73
    /**
74
     * Performs a 'bulk-report' api request, with Exception handling
75
     * 
76
     * @access public
77
     * @param string    $filePath       The CSV file path. Could be an absolute or relative path.
78
     *
79
     * @return ApiResponse
80
     */
81
    public function bulkReport(string $filePath): ApiResponse
82
    {
83
        try {
84
            return parent::bulkReport($filePath);
85
        } catch (\Exception $e) {
86
            return $this->getErrorResponse($e->getMessage());
87
        }
88
    }
89
90
    /**
91
     * Perform a 'clear-address' api request, with Exception handling
92
     * 
93
     * @access public
94
     * @param string    $ip             The IP to clear reports
95
     * 
96
     * @return ApiResponse
97
     */
98
    public function clearAddress(string $ip): ApiResponse
99
    {
100
        try {
101
            return parent::clearAddress($ip);
102
        } catch (\Exception $e) {
103
            return $this->getErrorResponse($e->getMessage());
104
        }
105
    }
106
107
    /**
108
     * Perform a 'check' api request, with Exception handling
109
     * 
110
     * @access public
111
     * @param string    $ip             The ip to check
112
     * @param int       $maxAgeInDays   Max age in days. Default is 30.
113
     * @param bool      $verbose        True to get the full response (last reports and countryName). Default is false
114
     * 
115
     * @return ApiResponse
116
     */
117
    public function check(string $ip, int $maxAgeInDays = 30, bool $verbose = false): ApiResponse
118
    {
119
        try {
120
            return parent::check($ip, $maxAgeInDays, $verbose);
121
        } catch (\Exception $e) {
122
            return $this->getErrorResponse($e->getMessage());
123
        }
124
    }
125
126
    /**
127
     * Perform a 'check-block' api request, with Exception handling
128
     * 
129
     * @access public
130
     * @param string    $network        The network to check
131
     * @param int       $maxAgeInDays   The Max age in days, must 
132
     * 
133
     * @return ApiResponse
134
     */
135
    public function checkBlock(string $network, int $maxAgeInDays = 30): ApiResponse
136
    {
137
        try {
138
            return parent::checkBlock($network, $maxAgeInDays);
139
        } catch (\Exception $e) {
140
            return $this->getErrorResponse($e->getMessage());
141
        }
142
    }
143
144
    /**
145
     * Perform a 'blacklist' api request, with Exception handling
146
     * 
147
     * @access public
148
     * @param int       $limit              The blacklist limit. Default is 10000 (the api default limit) 
149
     * @param bool      $plainText          True to get the response in plaintext list. Default is false
150
     * @param int       $confidenceMinimum  The abuse confidence score minimum (subscribers feature). Default is 100.
151
     *                                      The confidence minimum must be between 25 and 100.
152
     *                                      This parameter is subscriber feature (not honored otherwise).
153
     * 
154
     * @return ApiResponse
155
     */
156
    public function blacklist(int $limit = 10000, bool $plainText = false, int $confidenceMinimum = 100): ApiResponse
157
    {
158
        try {
159
            return parent::blacklist($limit, $plainText, $confidenceMinimum);
160
        } catch (\Exception $e) {
161
            return $this->getErrorResponse($e->getMessage());
162
        }
163
    }
164
}