Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaModerationFileFactory
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileObjectForRow
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2declare( strict_types=1 );
3
4namespace MediaWiki\Extension\MediaModeration\Services;
5
6use InvalidArgumentException;
7use MediaWiki\FileRepo\File\ArchivedFile;
8use MediaWiki\FileRepo\File\LocalFile;
9use MediaWiki\FileRepo\LocalRepo;
10
11/**
12 * A service that allows creating File or ArchivedFile objects
13 * for given rows from the image, oldimage, and filearchive tables.
14 */
15class MediaModerationFileFactory {
16
17    public function __construct(
18        private readonly LocalRepo $localRepo,
19    ) {
20    }
21
22    /**
23     * Get the LocalFile or ArchiveFile object for the $row.
24     * The exact object type depends on the $table provided.
25     *
26     * @param \stdClass $row
27     * @param string $table
28     * @return ArchivedFile|LocalFile
29     */
30    public function getFileObjectForRow( $row, string $table ) {
31        if ( $table === 'image' || $table === 'oldimage' ) {
32            return $this->localRepo->newFileFromRow( (object)$row );
33        } elseif ( $table === 'filearchive' ) {
34            return ArchivedFile::newFromRow( (object)$row );
35        } else {
36            throw new InvalidArgumentException( "Unrecognised image table '$table'." );
37        }
38    }
39}