Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.27% covered (warning)
77.27%
17 / 22
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
FileProvider
77.27% covered (warning)
77.27%
17 / 22
60.00% covered (warning)
60.00%
3 / 5
8.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFileFromTitle
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getFileForIndexTitle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFileForPageTitle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getPageNumberForPageTitle
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace ProofreadPage;
4
5use File;
6use MediaWiki\Title\Title;
7use RepoGroup;
8
9/**
10 * @license GPL-2.0-or-later
11 *
12 * Provide related file for various kind of pages
13 */
14class FileProvider {
15
16    /**
17     * @var RepoGroup the repositories to use
18     */
19    private $repoGroup;
20
21    /**
22     * @param RepoGroup $repoGroup the repositories to use
23     */
24    public function __construct( RepoGroup $repoGroup ) {
25        $this->repoGroup = $repoGroup;
26    }
27
28    /**
29     * @param Title $title
30     * @return File
31     * @throws FileNotFoundException
32     */
33    public function getFileFromTitle( Title $title ) {
34        $result = $this->repoGroup->findFile( $title );
35        if ( $result === false ) {
36            throw new FileNotFoundException();
37        }
38        return $result;
39    }
40
41    /**
42     * @param Title $indexTitle
43     * @return File
44     * @throws FileNotFoundException
45     */
46    public function getFileForIndexTitle( Title $indexTitle ) {
47        return $this->getFileFromTitle(
48            Title::makeTitle( NS_FILE, $indexTitle->getText() )
49        );
50    }
51
52    /**
53     * @param Title $pageTitle
54     * @return File
55     * @throws FileNotFoundException
56     */
57    public function getFileForPageTitle( Title $pageTitle ) {
58        // try to get an image with the same name as the file
59        return $this->getFileFromTitle(
60        // use the base name as file name
61            Title::makeTitle( NS_FILE, strtok( $pageTitle->getText(), '/' ) )
62        );
63    }
64
65    /**
66     * @param Title $pageTitle
67     * @return int
68     * @throws PageNumberNotFoundException
69     */
70    public function getPageNumberForPageTitle( Title $pageTitle ) {
71        $parts = explode( '/', $pageTitle->getText() );
72        if ( count( $parts ) === 1 ) {
73            throw new PageNumberNotFoundException(
74                $pageTitle->getFullText() . ' does not provide a page number.'
75            );
76        }
77        $number = (int)$pageTitle->getPageLanguage()->parseFormattedNumber( end( $parts ) );
78        if ( $number > 0 ) {
79            // Valid page numbers are integer > 0.
80            return $number;
81        }
82        throw new PageNumberNotFoundException(
83            $pageTitle->getFullText() . ' does not provide a valid page number.'
84        );
85    }
86}