Passed
Push — master ( 480b73...5a1eb5 )
by Arnaud
07:18 queued 16s
created

File::readExif()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0987

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 5
eloc 16
c 2
b 2
f 0
nc 9
nop 1
dl 0
loc 26
ccs 11
cts 17
cp 0.6471
crap 6.0987
rs 9.4222
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
19
class File
20
{
21
    /** @var Filesystem */
22
    protected static $fs;
23
24
    /**
25
     * Returns a Symfony\Component\Filesystem instance.
26
     */
27 1
    public static function getFS(): Filesystem
28
    {
29 1
        if (!self::$fs instanceof Filesystem) {
30 1
            self::$fs = new Filesystem();
31
        }
32
33 1
        return self::$fs;
34
    }
35
36
    /**
37
     * file_get_contents() function with error handler.
38
     *
39
     * @return string|false
40
     */
41 1
    public static function fileGetContents(string $filename, bool $userAgent = false)
42
    {
43 1
        if (empty($filename)) {
44
            return false;
45
        }
46
47 1
        set_error_handler(
48 1
            function ($severity, $message, $file, $line) {
49 1
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
50 1
            }
51 1
        );
52
53
        try {
54 1
            if ($userAgent) {
55 1
                $options = [
56 1
                    'http' => [
57 1
                        'method'          => 'GET',
58 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',
59 1
                        'follow_location' => true,
60 1
                    ],
61 1
                ];
62
63 1
                return file_get_contents($filename, false, stream_context_create($options));
64
            }
65
66 1
            return file_get_contents($filename);
67 1
        } catch (\ErrorException) {
68 1
            return false;
69
        } finally {
70 1
            restore_error_handler();
71
        }
72
    }
73
74
    /**
75
     * Returns MIME content type and subtype of a file.
76
     *
77
     * ie: ['text', 'text/plain']
78
     */
79 1
    public static function getMimeType(string $filename): array
80
    {
81 1
        if (false === $subtype = mime_content_type($filename)) {
82
            throw new RuntimeException(sprintf('Can\'t get MIME content type of "%s".', $filename));
83
        }
84 1
        $type = explode('/', $subtype)[0];
85
86 1
        return [
87 1
            $type,
88 1
            $subtype,
89 1
        ];
90
    }
91
92
    /**
93
     * exif_read_data() function with error handler.
94
     */
95 1
    public static function readExif(string $filename): array
96
    {
97 1
        if (empty($filename)) {
98
            return [];
99
        }
100
101 1
        set_error_handler(
102 1
            function ($severity, $message, $file, $line) {
103
                throw new \ErrorException($message, 0, $severity, $file, $line, null);
104 1
            }
105 1
        );
106
107
        try {
108 1
            if (!\function_exists('exif_read_data')) {
109
                throw new \ErrorException('`exif` extension is not available.');
110
            }
111 1
            $exif = exif_read_data($filename, null, true);
112 1
            if ($exif === false) {
113
                return [];
114
            }
115
116 1
            return $exif;
117
        } catch (\ErrorException) {
118
            return [];
119
        } finally {
120 1
            restore_error_handler();
121
        }
122
    }
123
}
124