Completed
Push — master ( cf1bf2...68106a )
by Freek
07:01
created

HasMediaTrait::deleteMedia()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary\HasMedia;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Spatie\MediaLibrary\Conversion\Conversion;
7
use Spatie\MediaLibrary\Events\CollectionHasBeenCleared;
8
use Spatie\MediaLibrary\Exceptions\MediaDoesNotBelongToModel;
9
use Spatie\MediaLibrary\Exceptions\MediaIsNotPartOfCollection;
10
use Spatie\MediaLibrary\Exceptions\UrlCouldNotBeOpened;
11
use Spatie\MediaLibrary\FileAdder\FileAdderFactory;
12
use Spatie\MediaLibrary\Filesystem;
13
use Spatie\MediaLibrary\Media;
14
use Spatie\MediaLibrary\MediaRepository;
15
16
trait HasMediaTrait
17
{
18
    /**
19
     * @var array
20
     */
21
    public $mediaConversions = [];
22
23
    /**
24
     * Set the polymorphic relation.
25
     *
26
     * @return mixed
27
     */
28
    public function media()
29
    {
30
        return $this->morphMany(config('laravel-medialibrary.media_model'), 'model');
31
    }
32
33
    /**
34
     * Add a file to the medialibrary. The file will be removed from
35
     * it's original location.
36
     *
37
     * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file
38
     *
39
     * @return \Spatie\MediaLibrary\FileAdder\FileAdder
40
     */
41
    public function addMedia($file)
42
    {
43
        return app(FileAdderFactory::class)->create($this, $file);
44
    }
45
46
    /**
47
     * Add a remote file to the medialibrary.
48
     *
49
     * @param $url
50
     *
51
     * @return \Spatie\MediaLibrary\FileAdder\FileAdder
52
     *
53
     * @throws \Spatie\MediaLibrary\Exceptions\UrlCouldNotBeOpened
54
     */
55
    public function addMediaFromUrl($url)
56
    {
57
        if (!$stream = @fopen($url, 'r')) {
58
            throw new UrlCouldNotBeOpened();
59
        }
60
61
        $tmpFile = tempnam(sys_get_temp_dir(), 'media-library');
62
        file_put_contents($tmpFile, $stream);
63
64
        $filename = basename(parse_url($url, PHP_URL_PATH));
65
66
        return app(FileAdderFactory::class)
67
            ->create($this, $tmpFile)
68
            ->usingName(pathinfo($filename, PATHINFO_FILENAME))
69
            ->usingFileName($filename);
70
    }
71
72
    /**
73
     * Copy a file to the medialibrary.
74
     *
75
     * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file
76
     *
77
     * @return \Spatie\MediaLibrary\FileAdder\FileAdder
78
     */
79
    public function copyMedia($file)
80
    {
81
        return $this->addMedia($file)->preservingOriginal();
82
    }
83
84
    /**
85
     * Determine if there is media in the given collection.
86
     *
87
     * @param $collectionName
88
     *
89
     * @return bool
90
     */
91
    public function hasMedia($collectionName = '')
92
    {
93
        return count($this->getMedia($collectionName)) ? true : false;
94
    }
95
96
    /**
97
     * Get media collection by its collectionName.
98
     *
99
     * @param string $collectionName
100
     * @param array  $filters
101
     *
102
     * @return \Illuminate\Support\Collection
103
     */
104
    public function getMedia($collectionName = '', $filters = [])
105
    {
106
        return app(MediaRepository::class)->getCollection($this, $collectionName, $filters);
107
    }
108
109
    /**
110
     * Get the first media item of a media collection.
111
     *
112
     * @param string $collectionName
113
     * @param array  $filters
114
     *
115
     * @return bool|Media
116
     */
117
    public function getFirstMedia($collectionName = 'default', $filters = [])
118
    {
119
        $media = $this->getMedia($collectionName, $filters);
120
121
        return count($media) ? $media->first() : false;
122
    }
123
124
    /**
125
     * Get the url of the image for the given conversionName
126
     * for first media for the given collectionName.
127
     * If no profile is given, return the source's url.
128
     *
129
     * @param string $collectionName
130
     * @param string $conversionName
131
     *
132
     * @return string
133
     */
134
    public function getFirstMediaUrl($collectionName = 'default', $conversionName = '')
135
    {
136
        $media = $this->getFirstMedia($collectionName);
137
138
        if (!$media) {
139
            return false;
140
        }
141
142
        return $media->getUrl($conversionName);
143
    }
144
145
    /**
146
     * Get the url of the image for the given conversionName
147
     * for first media for the given collectionName.
148
     * If no profile is given, return the source's url.
149
     *
150
     * @param string $collectionName
151
     * @param string $conversionName
152
     *
153
     * @return string
154
     */
155
    public function getFirstMediaPath($collectionName = 'default', $conversionName = '')
156
    {
157
        $media = $this->getFirstMedia($collectionName);
158
159
        if (!$media) {
160
            return false;
161
        }
162
163
        return $media->getPath($conversionName);
164
    }
165
166
    /**
167
     * Update a media collection by deleting and inserting again with new values.
168
     *
169
     * @param array  $newMediaArray
170
     * @param string $collectionName
171
     *
172
     * @return array
173
     *
174
     * @throws \Spatie\MediaLibrary\Exceptions\MediaIsNotPartOfCollection
175
     */
176
    public function updateMedia(array $newMediaArray, $collectionName = 'default')
177
    {
178
        $this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName);
179
180
        $orderColumn = 1;
181
182
        $updatedMedia = [];
183
        foreach ($newMediaArray as $newMediaItem) {
184
            $mediaClass = config('laravel-medialibrary.media_model');
185
            $currentMedia = $mediaClass::findOrFail($newMediaItem['id']);
186
187
            if ($currentMedia->collection_name != $collectionName) {
188
                throw new MediaIsNotPartOfCollection(
189
                    sprintf('Media id %s is not part of collection %s', $currentMedia->id, $collectionName)
190
                );
191
            }
192
193
            if (array_key_exists('name', $newMediaItem)) {
194
                $currentMedia->name = $newMediaItem['name'];
195
            }
196
197
            if (array_key_exists('custom_properties', $newMediaItem)) {
198
                $currentMedia->custom_properties = $newMediaItem['custom_properties'];
199
            }
200
201
            $currentMedia->order_column = $orderColumn++;
202
203
            $currentMedia->save();
204
205
            $updatedMedia[] = $currentMedia;
206
        }
207
208
        return $updatedMedia;
209
    }
210
211
    /**
212
     * @param array  $newMediaArray
213
     * @param string $collectionName
214
     */
215
    protected function removeMediaItemsNotPresentInArray(array $newMediaArray, $collectionName = 'default')
216
    {
217
        $this->getMedia($collectionName, [])
218
            ->filter(function ($currentMediaItem) use ($newMediaArray) {
219
                return !in_array($currentMediaItem->id, collect($newMediaArray)->lists('id')->toArray());
0 ignored issues
show
Deprecated Code introduced by
The method Illuminate\Support\Collection::lists() has been deprecated with message: since version 5.2. Use the "pluck" method directly.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
220
            })
221
            ->map(function ($media) {
222
                $media->delete();
223
            });
224
    }
225
226
    /**
227
     * Remove all media in the given collection.
228
     *
229
     * @param string $collectionName
230
     *
231
     * @return $this
232
     */
233
    public function clearMediaCollection($collectionName = 'default')
234
    {
235
        $this->getMedia($collectionName)->map(function ($media) {
236
            app(Filesystem::class)->removeFiles($media);
237
            $media->delete();
238
        });
239
240
        app(Dispatcher::class)->fire(new CollectionHasBeenCleared($this, $collectionName));
241
242
        return $this;
243
    }
244
245
    /**
246
     * Delete the associated media with the given id.
247
     * You may also pass a media object.
248
     *
249
     * @param int|\Spatie\MediaLibrary\Media $mediaId
250
     *
251
     * @throws \Spatie\MediaLibrary\Exceptions\MediaDoesNotBelongToModel
252
     */
253
    public function deleteMedia($mediaId)
254
    {
255
        if ($mediaId instanceof Media) {
256
            $mediaId = $mediaId->id;
257
        }
258
259
        $media = $this->media->find($mediaId);
260
261
        if (!$media) {
262
            throw new MediaDoesNotBelongToModel('Media id '.$mediaId.' does not belong to this model');
263
        }
264
265
        $media->delete();
266
    }
267
268
    /**
269
     * Add a conversion.
270
     *
271
     * @param string $name
272
     *
273
     * @return \Spatie\MediaLibrary\Conversion\Conversion
274
     */
275
    public function addMediaConversion($name)
276
    {
277
        $conversion = Conversion::create($name);
278
279
        $this->mediaConversions[] = $conversion;
280
281
        return $conversion;
282
    }
283
284
    /**
285
     * Delete the model. The extra logic isn't handled in a model event since the boot function is unreliable
286
     * for currently unknown reasons.
287
     *
288
     * @return bool
289
     */
290
    public function delete()
291
    {
292
        if (!parent::delete()) {
293
            return false;
294
        }
295
296
        $this->media()->get()->map(function ($media) {
297
            $media->delete();
298
        });
299
300
        return true;
301
    }
302
303
    /**
304
     * Delete the model, but preserve all the associated media.
305
     *
306
     * @return bool
307
     */
308
    public function deletePreservingMedia()
309
    {
310
        return parent::delete();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (delete() instead of deletePreservingMedia()). Are you sure this is correct? If so, you might want to change this to $this->delete().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
311
    }
312
}
313