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
2
3namespace MediaWiki\Extension\MediaModeration\Services;
4
5use ArchivedFile;
6use InvalidArgumentException;
7use LocalFile;
8use LocalRepo;
9
10/**
11 * A service that allows creating File or ArchivedFile objects
12 * for given rows from the image, oldimage, and filearchive tables.
13 */
14class MediaModerationFileFactory {
15
16    private LocalRepo $localRepo;
17
18    public function __construct( LocalRepo $localRepo ) {
19        $this->localRepo = $localRepo;
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}