Passed
Push — master ( 46ae56...237bda )
by
unknown
07:12
created

File::isRemote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Util;
15
16
use Cecil\Exception\RuntimeException;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Mime\MimeTypes;
19
20
class File
21
{
22
    /** @var Filesystem */
23
    protected static $fs;
24
25
    /**
26
     * Returns a Symfony\Component\Filesystem instance.
27
     */
28 1
    public static function getFS(): Filesystem
29
    {
30 1
        if (!self::$fs instanceof Filesystem) {
31 1
            self::$fs = new Filesystem();
32
        }
33
34 1
        return self::$fs;
35
    }
36
37
    /**
38
     * file_get_contents() function with error handler.
39
     *
40
     * @return string|false
41
     */
42 1
    public static function fileGetContents(string $filename, bool $userAgent = false)
43
    {
44 1
        if (empty($filename)) {
45
            return false;
46
        }
47
48 1
        set_error_handler(
49 1
            function ($severity, $message, $file, $line) {
50 1
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
51 1
            }
52 1
        );
53
54
        try {
55 1
            if ($userAgent) {
56 1
                $options = [
57 1
                    'http' => [
58 1
                        'method'          => 'GET',
59 1
                        'header'          => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36',
60 1
                        'follow_location' => true,
61 1
                    ],
62 1
                ];
63
64 1
                return file_get_contents($filename, false, stream_context_create($options));
65
            }
66
67 1
            return file_get_contents($filename);
68 1
        } catch (\ErrorException) {
69 1
            return false;
70
        } finally {
71 1
            restore_error_handler();
72
        }
73
    }
74
75
    /**
76
     * Returns the media type and subtype of a file.
77
     *
78
     * ie: ['text', 'text/plain']
79
     */
80 1
    public static function getMediaType(string $filename): array
81
    {
82
        try {
83 1
            if (false !== $subtype = mime_content_type($filename)) {
84 1
                return [explode('/', $subtype)[0], $subtype];
85
            }
86
            $mimeTypes = new MimeTypes();
87
            $subtype = $mimeTypes->guessMimeType($filename);
88
            if ($subtype === null) {
89
                throw new RuntimeException('Can\'t guess the media type.');
90
            }
91
92
            return [explode('/', $subtype)[0], $subtype];
93
        } catch (\Exception $e) {
94
            throw new RuntimeException(\sprintf('Can\'t get media type of "%s" (%s).', $filename, $e->getMessage()));
95
        }
96
    }
97
98
    /**
99
     * Returns the extension of a file.
100
     */
101 1
    public static function getExtension(string $filename): string
102
    {
103
        try {
104 1
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
105 1
            if (!empty($ext)) {
106 1
                return $ext;
107
            }
108
            // guess the extension
109 1
            $mimeTypes = new MimeTypes();
110 1
            $mimeType = $mimeTypes->guessMimeType($filename);
111 1
            if ($mimeType === null) {
112
                throw new RuntimeException('Can\'t guess the media type.');
113
            }
114 1
            $exts = $mimeTypes->getExtensions($mimeType);
115
116 1
            return $exts[0];
117
        } catch (\Exception $e) {
118
            throw new RuntimeException(
119
                \sprintf('Can\'t get extension of "%s".', $filename),
120
                previous: $e,
121
            );
122
        }
123
    }
124
125
    /**
126
     * exif_read_data() function with error handler.
127
     */
128 1
    public static function readExif(string $filename): array
129
    {
130 1
        if (empty($filename)) {
131
            return [];
132
        }
133
134 1
        set_error_handler(
135 1
            function ($severity, $message, $file, $line) {
136
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
137 1
            }
138 1
        );
139
140
        try {
141 1
            if (!\function_exists('exif_read_data')) {
142
                throw new \ErrorException('`exif` extension is not available.');
143
            }
144 1
            $exif = exif_read_data($filename, null, true);
145 1
            if ($exif === false) {
146
                return [];
147
            }
148
149 1
            return $exif;
150
        } catch (\ErrorException) {
151
            return [];
152
        } finally {
153 1
            restore_error_handler();
154
        }
155
    }
156
157
    /**
158
     * Returns the real path of a relative file path.
159
     */
160 1
    public static function getRealPath(string $path): string
161
    {
162
        // if file exists
163 1
        $filePath = realpath(\Cecil\Util::joinFile(__DIR__, '/../', $path));
164 1
        if ($filePath !== false) {
165 1
            return $filePath;
166
        }
167
        // if Phar
168 1
        if (Platform::isPhar()) {
169
            return \Cecil\Util::joinPath(Platform::getPharPath(), str_replace('../', '/', $path));
170
        }
171
172 1
        throw new RuntimeException(\sprintf('Can\'t get the real path of file "%s".', $path));
173
    }
174
175
    /**
176
     * Tests if a file path is remote.
177
     */
178 1
    public static function isRemote(string $path): bool
179
    {
180 1
        return (bool) preg_match('~^(?:f|ht)tps?://~i', $path);
181
    }
182
183
    /**
184
     * Tests if a remote file exists.
185
     */
186 1
    public static function isRemoteExists(string $path): bool
187
    {
188 1
        if (self::isRemote($path)) {
189 1
            $handle = @fopen($path, 'r');
190 1
            if (!empty($http_response_header)) {
191 1
                if (400 < (int) explode(' ', $http_response_header[0])[1]) {
192 1
                    return false;
193
                }
194
            }
195 1
            if (\is_resource($handle)) {
196 1
                return true;
197
            }
198
        }
199
200
        return false;
201
    }
202
}
203