Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
39 / 39 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
MediaModerationPhotoDNAResponseHandler | |
100.00% |
39 / 39 |
|
100.00% |
1 / 1 |
8 | |
100.00% |
1 / 1 |
createStatusFromResponse | |
100.00% |
39 / 39 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MediaModeration\PhotoDNA; |
4 | |
5 | use MediaWiki\Language\RawMessage; |
6 | use StatusValue; |
7 | |
8 | /** |
9 | * Common methods shared across mock and real implementations of PhotoDNA endpoint. |
10 | */ |
11 | trait MediaModerationPhotoDNAResponseHandler { |
12 | |
13 | /** |
14 | * Return an appropriate status value, given a response from PhotoDNA endpoint. |
15 | * |
16 | * @param Response $response |
17 | * @return StatusValue |
18 | * The StatusValue always contains the Response object constructed from the API, which includes |
19 | * - IsMatch |
20 | * - StatusCode (from PhotoDNA) |
21 | * - raw response JSON |
22 | * |
23 | * Fatal status values are used to handle the Response::STATUS_* codes that indicate errors |
24 | * on the PhotoDNA API side. RawMessage is used throughout, as we don't anticipate |
25 | * showing these errors to users. |
26 | */ |
27 | private function createStatusFromResponse( Response $response ): StatusValue { |
28 | $status = StatusValue::newGood( $response ); |
29 | switch ( $response->getStatusCode() ) { |
30 | case Response::STATUS_OK: |
31 | return $status; |
32 | case Response::STATUS_INVALID_MISSING_REQUEST_PARAMS: |
33 | return $status->merge( StatusValue::newFatal( |
34 | new RawMessage( |
35 | $response->getStatusCode() . |
36 | ': Invalid or missing request parameter(s)' |
37 | ) |
38 | ) ); |
39 | case Response::STATUS_UNKNOWN_SCENARIO: |
40 | return $status->merge( StatusValue::newFatal( |
41 | new RawMessage( |
42 | $response->getStatusCode() . |
43 | ': Unknown scenario or unhandled error occurred while processing request' |
44 | ) |
45 | ) ); |
46 | case Response::STATUS_COULD_NOT_VERIFY_FILE_AS_IMAGE: |
47 | return $status->merge( StatusValue::newFatal( |
48 | new RawMessage( |
49 | $response->getStatusCode() . |
50 | ': The given file could not be verified as an image' |
51 | ) |
52 | ) ); |
53 | case Response::STATUS_IMAGE_PIXEL_SIZE_NOT_IN_RANGE: |
54 | return $status->merge( StatusValue::newFatal( |
55 | new RawMessage( |
56 | $response->getStatusCode() . |
57 | ': Image size in pixels is not within allowed range' |
58 | ) |
59 | ) ); |
60 | case Response::STATUS_REQUEST_SIZE_EXCEEDED: |
61 | return $status->merge( StatusValue::newFatal( |
62 | new RawMessage( |
63 | $response->getStatusCode() . |
64 | ': Request Size Exceeded' |
65 | ) |
66 | ) ); |
67 | default: |
68 | return $status->merge( StatusValue::newFatal( |
69 | new RawMessage( |
70 | $response->getStatusCode() . |
71 | ': Unknown status code' |
72 | ) |
73 | ) ); |
74 | } |
75 | } |
76 | |
77 | } |