Completed
Push — master ( f2fe55...d64f0f )
by Freek
17:48
created

FileAdder   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 355
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 17
Bugs 3 Features 3
Metric Value
wmc 25
c 17
b 3
f 3
lcom 1
cbo 6
dl 0
loc 355
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setSubject() 0 6 1
B setFile() 0 30 4
A preservingOriginal() 0 6 1
A usingName() 0 4 1
A setName() 0 6 1
A usingFileName() 0 4 1
A setFileName() 0 6 1
A withCustomProperties() 0 6 1
A withProperties() 0 6 1
A withAttributes() 0 4 1
A toMediaLibrary() 0 4 1
A toMediaLibraryOnDisk() 0 4 1
A toCollection() 0 4 1
B toCollectionOnDisk() 0 35 4
A determineDiskName() 0 12 3
A sanitizeFileName() 0 4 1
1
<?php
2
3
namespace Spatie\MediaLibrary\FileAdder;
4
5
use Illuminate\Contracts\Cache\Repository;
6
use Spatie\MediaLibrary\Exceptions\FileCannotBeImported;
7
use Spatie\MediaLibrary\Exceptions\FileDoesNotExist;
8
use Spatie\MediaLibrary\Exceptions\FileTooBig;
9
use Spatie\MediaLibrary\Exceptions\FilesystemDoesNotExist;
10
use Spatie\MediaLibrary\Filesystem;
11
use Spatie\MediaLibrary\Media;
12
use Symfony\Component\HttpFoundation\File\File;
13
use Symfony\Component\HttpFoundation\File\UploadedFile;
14
15
class FileAdder
16
{
17
    /**
18
     * @var \Illuminate\Database\Eloquent\Model subject
19
     */
20
    protected $subject;
21
22
    /**
23
     * @var Filesystem
24
     */
25
    protected $fileSystem;
26
27
    /**
28
     * @var Repository
29
     */
30
    protected $config;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $preserveOriginal = false;
36
37
    /**
38
     * @var string|\Symfony\Component\HttpFoundation\File\UploadedFile
39
     */
40
    protected $file;
41
42
    /**
43
     * @var array
44
     */
45
    protected $properties = [];
46
47
    /**
48
     * @var array
49
     */
50
    protected $customProperties = [];
51
52
    /**
53
     * @var string
54
     */
55
    protected $pathToFile;
56
57
    /**
58
     * @var string
59
     */
60
    protected $fileName;
61
62
    /**
63
     * @var string
64
     */
65
    protected $mediaName;
66
67
    /**
68
     * @var string
69
     */
70
    protected $diskName = '';
71
72
    /**
73
     * @param Filesystem $fileSystem
74
     * @param Repository $config
75
     */
76
    public function __construct(Filesystem $fileSystem, Repository $config)
77
    {
78
        $this->fileSystem = $fileSystem;
79
        $this->config = $config;
80
    }
81
82
    /**
83
     * @param \Illuminate\Database\Eloquent\Model $subject
84
     *
85
     * @return FileAdder
86
     */
87
    public function setSubject($subject)
88
    {
89
        $this->subject = $subject;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Set the file that needs to be imported.
96
     *
97
     * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file
98
     *
99
     * @return $this
100
     *
101
     * @throws FileCannotBeImported
102
     */
103
    public function setFile($file)
104
    {
105
        $this->file = $file;
106
107
        if (is_string($file)) {
108
            $this->pathToFile = $file;
109
            $this->setFileName(pathinfo($file, PATHINFO_BASENAME));
110
            $this->mediaName = pathinfo($file, PATHINFO_FILENAME);
111
112
            return $this;
113
        }
114
115
        if ($file instanceof UploadedFile) {
116
            $this->pathToFile = $file->getPath().'/'.$file->getFilename();
117
            $this->setFileName($file->getClientOriginalName());
118
            $this->mediaName = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
119
120
            return $this;
121
        }
122
123
        if ($file instanceof File) {
124
            $this->pathToFile = $file->getPath().'/'.$file->getFilename();
125
            $this->setFileName(pathinfo($file->getFilename(), PATHINFO_BASENAME));
126
            $this->mediaName = pathinfo($file->getFilename(), PATHINFO_FILENAME);
127
128
            return $this;
129
        }
130
131
        throw new FileCannotBeImported('Only strings, FileObjects and UploadedFileObjects can be imported');
132
    }
133
134
    /**
135
     * When adding the file to the media library, the original file
136
     * will be preserved.
137
     *
138
     * @return $this
139
     */
140
    public function preservingOriginal()
141
    {
142
        $this->preserveOriginal = true;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Set the name of the media object.
149
     *
150
     * @param $name
151
     *
152
     * @return $this
153
     */
154
    public function usingName($name)
155
    {
156
        return $this->setName($name);
157
    }
158
159
    /**
160
     * Set the name of the media object.
161
     *
162
     * @param $name
163
     *
164
     * @return $this
165
     */
166
    public function setName($name)
167
    {
168
        $this->mediaName = $name;
169
170
        return $this;
171
    }
172
173
    /**
174
     * Set the name of the file that is stored on disk.
175
     *
176
     * @param $fileName
177
     *
178
     * @return $this
179
     */
180
    public function usingFileName($fileName)
181
    {
182
        return $this->setFileName($fileName);
183
    }
184
185
    /**
186
     * Set the name of the file that is stored on disk.
187
     *
188
     * @param $fileName
189
     *
190
     * @return $this
191
     */
192
    public function setFileName($fileName)
193
    {
194
        $this->fileName = $this->sanitizeFileName($fileName);
195
196
        return $this;
197
    }
198
199
    /**
200
     * Set the metadata.
201
     *
202
     * @param array $customProperties
203
     *
204
     * @return $this
205
     */
206
    public function withCustomProperties(array $customProperties)
207
    {
208
        $this->customProperties = $customProperties;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Set properties on the model.
215
     *
216
     * @param array $properties
217
     *
218
     * @return $this
219
     */
220
    public function withProperties(array $properties)
221
    {
222
        $this->properties = $properties;
223
224
        return $this;
225
    }
226
227
    /**
228
     * Set attributes on the model.
229
     *
230
     * @param array $properties
231
     *
232
     * @return $this
233
     */
234
    public function withAttributes(array $properties)
235
    {
236
        return $this->withProperties($properties);
237
    }
238
239
    /**
240
     * Set the target media collection to default.
241
     * Will also start the import process.
242
     *
243
     * @param string $collectionName
244
     * @param string $diskName
245
     *
246
     * @return Media
247
     *
248
     * @throws FileDoesNotExist
249
     * @throws FileTooBig
250
     */
251
    public function toMediaLibrary($collectionName = 'default', $diskName = '')
252
    {
253
        return $this->toCollectionOnDisk($collectionName, $diskName);
254
    }
255
256
    /**
257
     * Set the target media collection to default.
258
     * Will also start the import process.
259
     *
260
     * @param string $collectionName
261
     * @param string $diskName
262
     *
263
     * @return Media
264
     *
265
     * @throws FileDoesNotExist
266
     * @throws FileTooBig
267
     */
268
    public function toMediaLibraryOnDisk($collectionName = 'default', $diskName = '')
269
    {
270
        return $this->toCollectionOnDisk($collectionName, $diskName);
271
    }
272
273
    /**
274
     * Set the collection name where to import the file.
275
     * Will also start the import process.
276
     *
277
     * @param string $collectionName
278
     * @param string $diskName
279
     *
280
     * @return Media
281
     *
282
     * @throws FileDoesNotExist
283
     * @throws FileTooBig
284
     */
285
    public function toCollection($collectionName = 'default', $diskName = '')
286
    {
287
        return $this->toCollectionOnDisk($collectionName, $diskName);
288
    }
289
290
    /**
291
     * @param string $collectionName
292
     * @param string $diskName
293
     *
294
     * @return \Spatie\MediaLibrary\Media
295
     *
296
     * @throws \Spatie\MediaLibrary\Exceptions\FileDoesNotExist
297
     * @throws \Spatie\MediaLibrary\Exceptions\FileTooBig
298
     * @throws \Spatie\MediaLibrary\Exceptions\FilesystemDoesNotExist
299
     */
300
    public function toCollectionOnDisk($collectionName = 'default', $diskName = '')
301
    {
302
        if (!is_file($this->pathToFile)) {
303
            throw new FileDoesNotExist();
304
        }
305
306
        if (filesize($this->pathToFile) > config('laravel-medialibrary.max_file_size')) {
307
            throw new FileTooBig();
308
        }
309
310
        $mediaClass = config('laravel-medialibrary.media_model');
311
        $media = new $mediaClass();
312
313
        $media->name = $this->mediaName;
314
        $media->file_name = $this->fileName;
315
        $media->disk = $this->determineDiskName($diskName);
316
317
        $media->collection_name = $collectionName;
318
319
        $media->size = filesize($this->pathToFile);
320
        $media->custom_properties = $this->customProperties;
321
        $media->manipulations = [];
322
323
        $media->fill($this->properties);
324
325
        $this->subject->media()->save($media);
326
327
        $this->fileSystem->add($this->pathToFile, $media, $this->fileName);
328
329
        if (!$this->preserveOriginal) {
330
            unlink($this->pathToFile);
331
        }
332
333
        return $media;
334
    }
335
336
    /**
337
     * Determine the disk to be used.
338
     *
339
     * @param string $diskName
340
     *
341
     * @return string
342
     *
343
     * @throws FilesystemDoesNotExist
344
     */
345
    protected function determineDiskName($diskName)
346
    {
347
        if ($diskName == '') {
348
            $diskName = config('laravel-medialibrary.defaultFilesystem');
349
        }
350
351
        if (is_null(config("filesystems.disks.{$diskName}"))) {
352
            throw new FilesystemDoesNotExist("There is no filesystem named {$diskName}");
353
        }
354
355
        return $diskName;
356
    }
357
358
    /**
359
     * Sanitize the given file name.
360
     *
361
     * @param $fileName
362
     *
363
     * @return string
364
     */
365
    protected function sanitizeFileName($fileName)
366
    {
367
        return str_replace(['#', '/', '\\'], '-', $fileName);
368
    }
369
}
370