Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
IPoidResponse | |
100.00% |
30 / 30 |
|
100.00% |
9 / 9 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
newFromArray | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
getBehaviors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRisks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTunnelOperators | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProxies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNumUsersOnThisIP | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCountries | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
jsonSerialize | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\IPReputation; |
4 | |
5 | use JsonSerializable; |
6 | |
7 | /** |
8 | * Value object returned by {@link IPReputationIPoidDataLookup::getIPoidDataForIp} |
9 | */ |
10 | class IPoidResponse implements JsonSerializable { |
11 | |
12 | /** @var string[]|null */ |
13 | private ?array $behaviors; |
14 | |
15 | /** @var string[] */ |
16 | private array $risks; |
17 | |
18 | /** @var string[]|null */ |
19 | private ?array $tunnelOperators; |
20 | |
21 | /** @var string[]|null */ |
22 | private ?array $proxies; |
23 | |
24 | private ?int $numUsersOnThisIP; |
25 | private ?int $countries; |
26 | |
27 | /** |
28 | * @param string[]|null $behaviors |
29 | * @param string[] $risks |
30 | * @param string[]|null $tunnelOperators |
31 | * @param string[]|null $proxies |
32 | * @param int|null $numUsersOnThisIP |
33 | * @param int|null $countries |
34 | */ |
35 | private function __construct( |
36 | ?array $behaviors, |
37 | array $risks, |
38 | ?array $tunnelOperators, |
39 | ?array $proxies, |
40 | ?int $numUsersOnThisIP, |
41 | ?int $countries |
42 | ) { |
43 | $this->behaviors = $behaviors; |
44 | $this->risks = $risks; |
45 | $this->tunnelOperators = $tunnelOperators; |
46 | $this->proxies = $proxies; |
47 | $this->numUsersOnThisIP = $numUsersOnThisIP; |
48 | $this->countries = $countries; |
49 | } |
50 | |
51 | /** |
52 | * Convert raw data from the IPoid service into an {@link IPoidResponse} object. |
53 | * |
54 | * @param array $data IP data returned by IPInfo |
55 | * |
56 | * @return self |
57 | * @internal For use by {@link IPReputationIPoidDataLookup::getIPoidDataForIp} and tests |
58 | */ |
59 | public static function newFromArray( array $data ): IPoidResponse { |
60 | if ( !isset( $data['risks'] ) || !$data['risks'] ) { |
61 | // 'risks' should always be set and populated, but if not set to 'UNKNOWN'. |
62 | $data['risks'] = [ 'UNKNOWN' ]; |
63 | } |
64 | |
65 | return new IPoidResponse( |
66 | $data['behaviors'] ?? null, |
67 | $data['risks'], |
68 | $data['tunnels'] ?? null, |
69 | $data['proxies'] ?? null, |
70 | $data['client_count'] ?? null, |
71 | $data['countries'] ?? null |
72 | ); |
73 | } |
74 | |
75 | /** |
76 | * @return string[]|null |
77 | */ |
78 | public function getBehaviors(): ?array { |
79 | return $this->behaviors; |
80 | } |
81 | |
82 | /** |
83 | * @return string[] |
84 | */ |
85 | public function getRisks(): array { |
86 | return $this->risks; |
87 | } |
88 | |
89 | /** |
90 | * @return string[]|null |
91 | */ |
92 | public function getTunnelOperators(): ?array { |
93 | return $this->tunnelOperators; |
94 | } |
95 | |
96 | /** |
97 | * @return string[]|null |
98 | */ |
99 | public function getProxies(): ?array { |
100 | return $this->proxies; |
101 | } |
102 | |
103 | public function getNumUsersOnThisIP(): ?int { |
104 | return $this->numUsersOnThisIP; |
105 | } |
106 | |
107 | public function getCountries(): ?int { |
108 | return $this->countries; |
109 | } |
110 | |
111 | public function jsonSerialize(): array { |
112 | return [ |
113 | 'behaviors' => $this->getBehaviors(), |
114 | 'risks' => $this->getRisks(), |
115 | 'tunnelOperators' => $this->getTunnelOperators(), |
116 | 'proxies' => $this->getProxies(), |
117 | 'numUsersOnThisIP' => $this->getNumUsersOnThisIP(), |
118 | 'countries' => $this->getCountries(), |
119 | ]; |
120 | } |
121 | } |