Passed
Push — main ( ee116e...54274e )
by Ahmad
02:12
created

File   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 121
rs 10
c 1
b 0
f 0
wmc 27

11 Methods

Rating   Name   Duplication   Size   Complexity  
A toVisionFile() 0 21 5
A getLocalPathname() 0 19 5
A __construct() 0 2 1
A getStreamFilePath() 0 10 2
A getTempDir() 0 3 1
A isGoogleStoragePath() 0 3 2
A createTempFile() 0 3 1
A isResource() 0 3 1
A isLocalFile() 0 3 2
A saveStream() 0 4 2
A getContents() 0 16 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AhmadMayahi\Vision\Support;
6
7
use AhmadMayahi\Vision\Contracts\File as FileContract;
8
use AhmadMayahi\Vision\Exceptions\FileException;
9
use AhmadMayahi\Vision\Exceptions\UnsupportedFileTypeException;
10
use Exception;
11
use SplFileInfo;
12
use SplFileObject;
13
14
class File implements FileContract
15
{
16
    /**
17
     * @param string|resource|SplFileInfo|SplFileObject $file Input file
18
     * @param null|string $tempDirPath Full path to temporary directory
19
     */
20
    public function __construct(private $file, private ?string $tempDirPath = null)
21
    {
22
    }
23
24
    /**
25
     * Generates vision compatible to be used as a file input in Google\Cloud\Vision\V1\ImageAnnotatorClient
26
     *
27
     * @return resource|string
28
     * @throws FileException
29
     */
30
    public function toVisionFile()
31
    {
32
        if (is_resource($this->file)) {
33
            return $this->file;
34
        }
35
36
        if ($this->file instanceof SplFileInfo) {
37
            $fileObj = $this->file->openFile();
38
39
            return (string) $fileObj->fread($this->file->getSize());
40
        }
41
42
        if ($this->isGoogleStoragePath()) {
43
            return (string) $this->file;
44
        }
45
46
        if (file_exists($this->file)) {
47
            return (string) file_get_contents($this->file);
48
        }
49
50
        throw new FileException('File not found or not compatible!');
51
    }
52
53
    public function getLocalPathname(): string
54
    {
55
        if ($this->isGoogleStoragePath()) {
56
            throw new Exception('Google Storage is not supported for this operation!');
57
        }
58
59
        if (is_resource($this->file)) {
60
            return $this->getStreamFilePath();
61
        }
62
63
        if ($this->file instanceof SplFileInfo) {
64
            return $this->file->getPathname();
65
        }
66
67
        if (file_exists($this->file)) {
68
            return $this->file;
69
        }
70
71
        throw new FileException('Cannot get the local file path');
72
    }
73
74
    public function getStreamFilePath(): string
75
    {
76
        if (false === $this->isResource()) {
77
            throw new FileException('File is not resource!');
78
        }
79
80
        $tempFile = $this->createTempFile();
81
        $this->saveStream($tempFile);
82
83
        return $tempFile;
84
    }
85
86
    private function createTempFile(): string
87
    {
88
        return tempnam($this->getTempDir(), sha1(uniqid()));
89
    }
90
91
    private function saveStream(string $tempFile)
92
    {
93
        if (false === file_put_contents($tempFile, stream_get_contents($this->file))) {
94
            throw new FileException('Could not write teo temp file: '.$tempFile);
95
        }
96
    }
97
98
    public function getContents()
99
    {
100
        if ($this->isGoogleStoragePath()) {
101
            throw new UnsupportedFileTypeException('Google Storage is not supported!');
102
        }
103
104
        if ($this->isLocalFile()) {
105
            return file_get_contents($this->file);
106
        }
107
108
        if (is_resource($this->file)) {
109
            return stream_get_contents($this->file);
110
        }
111
112
        if ($this->file instanceof SplFileInfo) {
113
            return file_get_contents($this->file->getPathname());
114
        }
115
    }
116
117
    public function isGoogleStoragePath(): bool
118
    {
119
        return is_string($this->file) && str_starts_with($this->file, 'gs://');
120
    }
121
122
    public function isResource(): bool
123
    {
124
        return is_resource($this->file);
125
    }
126
127
    public function isLocalFile(): bool
128
    {
129
        return is_string($this->file) && file_exists($this->file);
130
    }
131
132
    private function getTempDir(): string
133
    {
134
        return $this->tempDirPath ?? sys_get_temp_dir();
135
    }
136
}
137