Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
MediaModerationMockPhotoDNAServiceProvider
n/a
0 / 0
n/a
0 / 0
2
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
1
 check
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3namespace MediaWiki\Extension\MediaModeration\Services;
4
5use FormatJson;
6use MediaWiki\Extension\MediaModeration\PhotoDNA\IMediaModerationPhotoDNAServiceProvider;
7use MediaWiki\Extension\MediaModeration\PhotoDNA\MediaModerationPhotoDNAResponseHandler;
8use MediaWiki\Extension\MediaModeration\PhotoDNA\Response;
9use StatusValue;
10
11/**
12 * Mock implementation of PhotoDNA endpoint, for local development environments and CI.
13 * @codeCoverageIgnore This is for CI and local development, so does not need code coverage.
14 */
15class MediaModerationMockPhotoDNAServiceProvider implements IMediaModerationPhotoDNAServiceProvider {
16
17    use MediaModerationPhotoDNAResponseHandler;
18
19    private array $filesToIsMatchMap;
20    private array $filesToStatusCodeMap;
21
22    /**
23     * @param array $filesToIsMatchMap Map of file names to boolean values where true indicates
24     *   a match with PhotoDNA's database, and false (default) means no match
25     * @param array $filesToStatusCodeMap Map of file names to integer status codes,
26     *   see the Response::STATUS_ constants. Default is STATUS_OK.
27     */
28    public function __construct(
29        array $filesToIsMatchMap = [],
30        array $filesToStatusCodeMap = []
31    ) {
32        $this->filesToIsMatchMap = $filesToIsMatchMap;
33        $this->filesToStatusCodeMap = $filesToStatusCodeMap;
34    }
35
36    /** @inheritDoc */
37    public function check( $file ): StatusValue {
38        $statusCode = $this->filesToStatusCodeMap[ $file->getName() ] ?? Response::STATUS_OK;
39        $isMatch = $this->filesToIsMatchMap[ $file->getName() ] ?? false;
40        $response = new Response(
41            $statusCode,
42            $isMatch,
43            FormatJson::encode( 'Mock endpoint in use!' )
44        );
45        return $this->createStatusFromResponse( $response );
46    }
47
48}