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