Attachment::getMimeType()   A
last analyzed

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 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Fwolf\Tools\TtSync\PluginApi;
4
5
/**
6
 * @copyright   Copyright 2017 Fwolf
7
 * @license     https://opensource.org/licenses/MIT MIT
8
 */
9
class Attachment implements AttachmentInterface
10
{
11
    /**
12
     * Temp file name prefix when download
13
     */
14
    const TMP_FILE_PREFIX = 'tt';
15
16
17
    /**
18
     * Is this attachment downloaded to disk or not ?
19
     *
20
     * @var bool
21
     */
22
    protected $isDownloaded = false;
23
24
    /**
25
     * @var string
26
     */
27
    protected $mimeType = '';
28
29
    /**
30
     * Path on disk
31
     *
32
     * Maybe empty if not download.
33
     *
34
     * @var string
35
     */
36
    protected $path = '';
37
38
    /**
39
     * @var int
40
     */
41
    protected $size = 0;
42
43
    /**
44
     * Url if its create from internet
45
     *
46
     * Maybe empty if load from disk.
47
     *
48
     * @var string
49
     */
50
    protected $url = '';
51
52
53
    /**
54
     * @param   string $urlOrPath
55
     */
56
    public function __construct(string $urlOrPath)
57
    {
58
        $this->setUrlOrPath($urlOrPath);
59
    }
60
61
62
    /**
63
     * @inheritdoc
64
     *
65
     * Will download attachment if haven't.
66
     */
67
    public function getMimeType(): string
68
    {
69
        if (!$this->isDownloaded()) {
70
            $this->save();
71
        }
72
73
        return $this->mimeType;
74
    }
75
76
77
    /**
78
     * @inheritdoc
79
     *
80
     * Will download attachment if haven't.
81
     */
82
    public function getPath(): string
83
    {
84
        if (!$this->isDownloaded()) {
85
            $this->save();
86
        }
87
88
        return $this->path;
89
    }
90
91
92
    /**
93
     * @inheritdoc
94
     *
95
     * Will download attachment if haven't.
96
     */
97
    public function getSize(): int
98
    {
99
        if (!$this->isDownloaded()) {
100
            $this->save();
101
        }
102
103
        return $this->size;
104
    }
105
106
107
    /**
108
     * Generate tmp file path for download
109
     *
110
     * @return  string
111
     */
112
    protected function getTmpPath(): string
113
    {
114
        $path = tempnam(sys_get_temp_dir(), static::TMP_FILE_PREFIX);
115
116
        // Add file ext of url to path
117
        $url = $this->getUrl();
118
        $pos = strrpos($url, '.');
119
        if (false === $pos) {
120
            // Url have no ext, return original
121
            return $path;
122
        } else {
123
            return $path . '.' . substr($url, $pos + 1);
124
        }
125
    }
126
127
128
    /**
129
     * @inheritdoc
130
     */
131
    public function getUrl()
132
    {
133
        return $this->url;
134
    }
135
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function isDownloaded(): bool
141
    {
142
        return $this->isDownloaded;
143
    }
144
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function save(string $path = ''): AttachmentInterface
150
    {
151
        if ($this->isDownloaded()) {
152
            if (empty($path)) {
153
                // Already downloaded to tmp, do nothing
154
                return $this;
155
            } else {
156
                rename($this->getPath(), $path);
157
            }
158
        } else {
159
            if (empty($path)) {
160
                $path = $this->getTmpPath();
161
            }
162
            file_put_contents($path, fopen($this->getUrl(), 'rb'));
163
        }
164
165
        $this->setPath($path);
166
167
        return $this;
168
    }
169
170
171
    /**
172
     * @param   bool $isDownloaded
173
     * @return  $this
174
     */
175
    protected function setIsDownloaded(bool $isDownloaded): self
176
    {
177
        $this->isDownloaded = $isDownloaded;
178
179
        return $this;
180
    }
181
182
183
    /**
184
     * @param   string $mimeType
185
     * @return  $this
186
     */
187
    protected function setMimeType(string $mimeType): self
188
    {
189
        $this->mimeType = $mimeType;
190
191
        return $this;
192
    }
193
194
195
    /**
196
     * @inheritdoc
197
     */
198
    public function setPath(string $path): AttachmentInterface
199
    {
200
        $this->path = $path;
201
202
        $this->setIsDownloaded(true);
203
        $this->setMimeType(mime_content_type($path));
204
        $this->setSize(filesize($path));
205
206
        return $this;
207
    }
208
209
210
    /**
211
     * @param   int $size
212
     * @return  $this
213
     */
214
    protected function setSize(int $size): self
215
    {
216
        $this->size = $size;
217
218
        return $this;
219
    }
220
221
222
    /**
223
     * @inheritdoc
224
     *
225
     * Will clear downloaded status.
226
     */
227
    public function setUrl(string $url): AttachmentInterface
228
    {
229
        $this->url = $url;
230
231
        $this->setIsDownloaded(false);
232
233
        return $this;
234
    }
235
236
237
    /**
238
     * @param   string $urlOrPath
239
     * @return  $this
240
     */
241
    protected function setUrlOrPath(string $urlOrPath): self
242
    {
243
        if ('http://' == strtolower(substr($urlOrPath, 0, 7)) ||
244
            'https://' == strtolower(substr($urlOrPath, 0, 8))
245
        ) {
246
            $this->setUrl($urlOrPath);
247
        } else {
248
            $this->setPath($urlOrPath);
249
        }
250
251
        return $this;
252
    }
253
}
254