Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
9 / 9
MimeTypeConfigurator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
9 / 9
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getMimeTypes
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
6 / 6
 getFirstMimeTypeByFileExtension
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace AdvancedSearch;
use MimeAnalyzer;
/**
 * @license GPL-2.0-or-later
 */
class MimeTypeConfigurator {
    /**
     * @var MimeAnalyzer
     */
    private $mimeAnalyzer;
    /**
     * @param MimeAnalyzer $mimeAnalyzer
     */
    public function __construct( MimeAnalyzer $mimeAnalyzer ) {
        $this->mimeAnalyzer = $mimeAnalyzer;
    }
    /**
     * @param string[] $fileExtensions
     *
     * @return string[] List of file extension => MIME type.
     */
    public function getMimeTypes( array $fileExtensions ) {
        $mimeTypes = [];
        foreach ( $fileExtensions as $ext ) {
            $mimeType = $this->getFirstMimeTypeByFileExtension( $ext );
            if ( !isset( $mimeTypes[$mimeType] ) ) {
                $mimeTypes[$mimeType] = $ext;
            }
        }
        return array_flip( $mimeTypes );
    }
    /**
     * Uses MimeAnalyzer to determine the mimetype of a given file extension
     *
     * @param string $fileExtension
     * @return string First mime type associated with the given file extension
     */
    private function getFirstMimeTypeByFileExtension( $fileExtension ) {
        return explode( ' ', $this->mimeAnalyzer->getTypesForExtension( $fileExtension ), 2 )[0];
    }
}