Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
FilePageLookup
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
1 / 2
4.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFilePage
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2
3namespace Wikibase\MediaInfo\Services;
4
5use MediaWiki\Title\Title;
6use MediaWiki\Title\TitleFactory;
7use Wikibase\MediaInfo\DataModel\MediaInfoId;
8
9/**
10 * Lookup service for getting the Title of the page in the File namespace that corresponds
11 * to a given MediaInfoId.
12 *
13 * @license GPL-2.0-or-later
14 * @author Daniel Kinzler
15 */
16class FilePageLookup {
17
18    /**
19     * @var TitleFactory
20     */
21    private $titleFactory;
22
23    public function __construct( TitleFactory $titleFactory ) {
24        $this->titleFactory = $titleFactory;
25    }
26
27    /**
28     * @param MediaInfoId $mediaInfoId
29     *
30     * @return Title|null The Title of the page in the File namespace that corresponds to
31     * the given MediaInfo, or null if there is no such page.
32     */
33    public function getFilePage( MediaInfoId $mediaInfoId ) {
34        $pageId = $mediaInfoId->getNumericId();
35
36        $filePageTitle = $this->titleFactory->newFromID( $pageId );
37
38        if ( !$filePageTitle ) {
39            return null;
40        }
41
42        if ( $filePageTitle->inNamespace( NS_FILE ) ) {
43            return $filePageTitle;
44        }
45
46        return null;
47    }
48}