| Total Complexity | 12 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | class FinfoMimeTypeDetector implements MimeTypeDetector |
||
| 13 | { |
||
| 14 | private const INCONCLUSIVE_MIME_TYPES = ['application/x-empty', 'text/plain', 'text/x-asm']; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var finfo |
||
| 18 | */ |
||
| 19 | private $finfo; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var ExtensionToMimeTypeMap |
||
| 23 | */ |
||
| 24 | private $extensionMap; |
||
| 25 | |||
| 26 | 36 | public function __construct(string $magicFile = '', ExtensionToMimeTypeMap $extensionMap = null) |
|
| 27 | { |
||
| 28 | 36 | $this->finfo = new finfo(FILEINFO_MIME_TYPE, $magicFile); |
|
| 29 | 36 | $this->extensionMap = $extensionMap ?: new GeneratedExtensionToMimeTypeMap(); |
|
| 30 | 36 | } |
|
| 31 | |||
| 32 | 18 | public function detectMimeType(string $path, $contents): ?string |
|
| 33 | { |
||
| 34 | 18 | $mimeType = is_string($contents) |
|
| 35 | 12 | ? (@$this->finfo->buffer($contents) ?: null) |
|
| 36 | 18 | : null; |
|
| 37 | |||
| 38 | 18 | if ($mimeType !== null && ! in_array($mimeType, self::INCONCLUSIVE_MIME_TYPES)) { |
|
| 39 | 6 | return $mimeType; |
|
| 40 | } |
||
| 41 | |||
| 42 | 12 | return $this->detectMimeTypeFromPath($path); |
|
| 43 | } |
||
| 44 | |||
| 45 | 18 | public function detectMimeTypeFromPath(string $path): ?string |
|
| 50 | } |
||
| 51 | |||
| 52 | 6 | public function detectMimeTypeFromFile(string $path): ?string |
|
| 53 | { |
||
| 54 | 6 | return @$this->finfo->file($path) ?: null; |
|
| 55 | } |
||
| 56 | |||
| 57 | 6 | public function detectMimeTypeFromBuffer(string $contents): ?string |
|
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 |