Test Failed
Push — master ( d45889...cddc62 )
by Divine Niiquaye
11:28 queued 08:59
created

FileHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 5
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Handlers;
19
20
use Flight\Routing\Exceptions\InvalidControllerException;
21
use Psr\Http\Message\{ResponseFactoryInterface, ResponseInterface};
22
23
/**
24
 * Returns the contents from a file.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
final class FileHandler
29
{
30
    public const MIME_TYPE = [
31
        'txt' => 'text/plain',
32
        'htm' => 'text/html',
33
        'html' => 'text/html',
34
        'php' => 'text/html',
35
        'css' => 'text/css',
36
        'js' => 'application/javascript',
37
        'json' => 'application/json',
38
        'xml' => 'text/xml',
39
        'swf' => 'application/x-shockwave-flash',
40
        'flv' => 'video/x-flv',
41
        // images
42
        'png' => 'image/png',
43
        'jpe' => 'image/jpeg',
44
        'jpeg' => 'image/jpeg',
45
        'jpg' => 'image/jpeg',
46
        'gif' => 'image/gif',
47
        'bmp' => 'image/bmp',
48
        'ico' => 'image/vnd.microsoft.icon',
49
        'tiff' => 'image/tiff',
50
        'tif' => 'image/tiff',
51
        'svg' => 'image/svg+xml',
52
        'svgz' => 'image/svg+xml',
53
        // archives
54
        'zip' => 'application/zip',
55
        'rar' => 'application/x-rar-compressed',
56
        'exe' => 'application/x-msdownload',
57
        'msi' => 'application/x-msdownload',
58
        'cab' => 'application/vnd.ms-cab-compressed',
59
        // audio/video
60
        'mp3' => 'audio/mpeg',
61
        'qt' => 'video/quicktime',
62
        'mov' => 'video/quicktime',
63
        // adobe
64
        'pdf' => 'application/pdf',
65
        'psd' => 'image/vnd.adobe.photoshop',
66
        'ai' => 'application/postscript',
67
        'eps' => 'application/postscript',
68
        'ps' => 'application/postscript',
69
        // ms office
70
        'doc' => 'application/msword',
71
        'rtf' => 'application/rtf',
72
        'xls' => 'application/vnd.ms-excel',
73
        'ppt' => 'application/vnd.ms-powerpoint',
74
        'docx' => 'application/msword',
75
        'xlsx' => 'application/vnd.ms-excel',
76
        'pptx' => 'application/vnd.ms-powerpoint',
77
        // open office
78
        'odt' => 'application/vnd.oasis.opendocument.text',
79
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
80
    ];
81
82
    public function __construct(private string $filename, private ?string $mimeType = null)
83
    {
84
    }
85
86
    public function __invoke(ResponseFactoryInterface $factory): ResponseInterface
87
    {
88
        if (!\file_exists($view = $this->filename) || !$contents = \file_get_contents($view)) {
89
            throw new InvalidControllerException(\sprintf('Failed to fetch contents from file "%s"', $view));
90
        }
91
92
        if (empty($mime = $this->mimeType ?? self::MIME_TYPE[\pathinfo($view, \PATHINFO_EXTENSION)] ?? null)) {
93
            $mime = (new \finfo(\FILEINFO_MIME_TYPE))->file($view); // @codeCoverageIgnoreStart
94
95
            if (false === $mime) {
96
                throw new InvalidControllerException(\sprintf('Failed to detect mime type of file "%s"', $view));
97
            } // @codeCoverageIgnoreEnd
98
        }
99
100
        $response = $factory->createResponse()->withHeader('Content-Type', $mime);
101
        $response->getBody()->write($contents);
102
103
        return $response;
104
    }
105
}
106