Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
MimeTypeConfigurator | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMimeTypes | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getFirstMimeTypeByFileExtension | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace AdvancedSearch; |
4 | |
5 | use MimeAnalyzer; |
6 | |
7 | /** |
8 | * @license GPL-2.0-or-later |
9 | */ |
10 | class MimeTypeConfigurator { |
11 | |
12 | /** |
13 | * @var MimeAnalyzer |
14 | */ |
15 | private $mimeAnalyzer; |
16 | |
17 | /** |
18 | * @param MimeAnalyzer $mimeAnalyzer |
19 | */ |
20 | public function __construct( MimeAnalyzer $mimeAnalyzer ) { |
21 | $this->mimeAnalyzer = $mimeAnalyzer; |
22 | } |
23 | |
24 | /** |
25 | * @param string[] $fileExtensions |
26 | * |
27 | * @return string[] List of file extension => MIME type. |
28 | */ |
29 | public function getMimeTypes( array $fileExtensions ) { |
30 | $mimeTypes = []; |
31 | |
32 | foreach ( $fileExtensions as $ext ) { |
33 | $mimeType = $this->getFirstMimeTypeByFileExtension( $ext ); |
34 | if ( !isset( $mimeTypes[$mimeType] ) ) { |
35 | $mimeTypes[$mimeType] = $ext; |
36 | } |
37 | } |
38 | |
39 | return array_flip( $mimeTypes ); |
40 | } |
41 | |
42 | /** |
43 | * Uses MimeAnalyzer to determine the mimetype of a given file extension |
44 | * |
45 | * @param string $fileExtension |
46 | * @return string|null First mime type associated with the given file extension |
47 | */ |
48 | private function getFirstMimeTypeByFileExtension( $fileExtension ) { |
49 | return $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( $fileExtension ); |
50 | } |
51 | |
52 | } |