Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Response | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newFromArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
getStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isMatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRawResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MediaModeration\PhotoDNA; |
4 | |
5 | /** |
6 | * Plain value object for modeling responses from the PhotoDNA endpoint. |
7 | * |
8 | * Not all fields from the response are modeled as properties; however, `rawResponse` |
9 | * is set from the response content. |
10 | * |
11 | * @see https://developer.microsoftmoderator.com/docs/services/57c7426e2703740ec4c9f4c3/operations/57c7426f27037407c8cc69e6 |
12 | */ |
13 | class Response { |
14 | |
15 | // Not a status code used by PhotoDNA. This code indicates invalid JSON returned by the API. |
16 | public const INVALID_JSON_STATUS_CODE = -1; |
17 | // The following statuses are documented as status codes by PhotoDNA, |
18 | // check @see link above in class-level documentation block |
19 | public const STATUS_OK = 3000; |
20 | public const STATUS_INVALID_MISSING_REQUEST_PARAMS = 3002; |
21 | public const STATUS_UNKNOWN_SCENARIO = 3004; |
22 | public const STATUS_COULD_NOT_VERIFY_FILE_AS_IMAGE = 3206; |
23 | public const STATUS_IMAGE_PIXEL_SIZE_NOT_IN_RANGE = 3208; |
24 | // Not listed in API documentation, but seen in practice if one sends a file that is too large. |
25 | public const STATUS_REQUEST_SIZE_EXCEEDED = 3209; |
26 | private int $statusCode; |
27 | private bool $isMatch; |
28 | private string $rawResponse; |
29 | |
30 | /** |
31 | * @param int $statusCode |
32 | * @param bool $isMatch |
33 | * @param string $rawResponse |
34 | */ |
35 | public function __construct( |
36 | int $statusCode, |
37 | bool $isMatch = false, |
38 | string $rawResponse = '' |
39 | ) { |
40 | $this->statusCode = $statusCode; |
41 | $this->isMatch = $isMatch; |
42 | $this->rawResponse = $rawResponse; |
43 | } |
44 | |
45 | /** |
46 | * @param array $responseJson |
47 | * @param string $rawResponse |
48 | * @return self |
49 | */ |
50 | public static function newFromArray( array $responseJson, string $rawResponse = '' ): self { |
51 | return new self( |
52 | $responseJson['Status']['Code'] ?? self::INVALID_JSON_STATUS_CODE, |
53 | $responseJson['IsMatch'] ?? false, |
54 | $rawResponse |
55 | ); |
56 | } |
57 | |
58 | public function getStatusCode(): int { |
59 | return $this->statusCode; |
60 | } |
61 | |
62 | public function isMatch(): bool { |
63 | return $this->isMatch; |
64 | } |
65 | |
66 | public function getRawResponse(): string { |
67 | return $this->rawResponse; |
68 | } |
69 | |
70 | } |