Passed
Push — master ( a9db05...32ab90 )
by Caen
03:20 queued 13s
created

MediaFile::getContentLength()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Filesystem;
6
7
use Hyde\Hyde;
8
use Hyde\Facades\Config;
9
use Hyde\Framework\Exceptions\FileNotFoundException;
10
use Illuminate\Support\Str;
11
use function extension_loaded;
12
use function file_exists;
13
use function array_merge;
14
use function array_keys;
15
use function filesize;
16
use function implode;
17
use function pathinfo;
18
use function collect;
19
use function is_file;
20
use function sprintf;
21
use function glob;
22
23
/**
24
 * File abstraction for a project media file.
25
 */
26
class MediaFile extends ProjectFile
27
{
28
    /** @var array<string> The default extensions for media types */
29
    final public const EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
30
31
    /** @return array<string, \Hyde\Support\Filesystem\MediaFile> The array keys are the filenames relative to the _media/ directory */
32
    public static function all(): array
33
    {
34
        return static::discoverMediaAssetFiles();
35
    }
36
37
    /** @return array<string> Array of filenames relative to the _media/ directory */
38
    public static function files(): array
39
    {
40
        return array_keys(static::all());
41
    }
42
43
    public function getIdentifier(): string
44
    {
45
        return Str::after($this->getPath(), Hyde::getMediaDirectory().'/');
46
    }
47
48
    public function toArray(): array
49
    {
50
        return array_merge(parent::toArray(), [
51
            'length' => $this->getContentLength(),
52
            'mimeType' => $this->getMimeType(),
53
        ]);
54
    }
55
56
    public function getContentLength(): int
57
    {
58
        if (! is_file($this->getAbsolutePath())) {
59
            throw new FileNotFoundException(message: "Could not get the content length of file [$this->path]");
60
        }
61
62
        return filesize($this->getAbsolutePath());
63
    }
64
65
    public function getMimeType(): string
66
    {
67
        $extension = pathinfo($this->getAbsolutePath(), PATHINFO_EXTENSION);
68
69
        // See if we can find a mime type for the extension instead of
70
        // having to rely on a PHP extension and filesystem lookups.
71
        $lookup = [
72
            'txt'  => 'text/plain',
73
            'md'   => 'text/markdown',
74
            'html' => 'text/html',
75
            'css'  => 'text/css',
76
            'svg'  => 'image/svg+xml',
77
            'png'  => 'image/png',
78
            'jpg'  => 'image/jpeg',
79
            'jpeg' => 'image/jpeg',
80
            'gif'  => 'image/gif',
81
            'json' => 'application/json',
82
            'js'   => 'application/javascript',
83
            'xml'  => 'application/xml',
84
        ];
85
86
        if (isset($lookup[$extension])) {
87
            return $lookup[$extension];
88
        }
89
90
        if (extension_loaded('fileinfo') && file_exists($this->getAbsolutePath())) {
91
            return mime_content_type($this->getAbsolutePath());
92
        }
93
94
        return 'text/plain';
95
    }
96
97
    protected static function discoverMediaAssetFiles(): array
98
    {
99
        return collect(static::getMediaAssetFiles())->mapWithKeys(function (string $filepath): array {
0 ignored issues
show
Bug introduced by
static::getMediaAssetFiles() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        return collect(/** @scrutinizer ignore-type */ static::getMediaAssetFiles())->mapWithKeys(function (string $filepath): array {
Loading history...
100
            $file = static::make($filepath);
101
102
            return [$file->getIdentifier() => $file];
103
        })->all();
104
    }
105
106
    protected static function getMediaAssetFiles(): array
107
    {
108
        return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
109
    }
110
111
    protected static function getMediaGlobPattern(): string
112
    {
113
        return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
114
            Config::getArray('hyde.media_extensions', self::EXTENSIONS)
115
        ));
116
    }
117
}
118