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