SavesToAmazonS3   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 12
c 4
b 0
f 1
dl 0
loc 26
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A saveFiles() 0 17 5
1
<?php
2
3
namespace FaithGen\SDK\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
10
trait SavesToAmazonS3
11
{
12
    use FileTraits;
13
14
    /**
15
     * Saves model images to Amazon s3.
16
     *
17
     * @param $model
18
     */
19
    protected function saveFiles($model)
20
    {
21
        if (! in_array(StorageTrait::class, class_uses($model))) {
22
            throw new InvalidArgumentException('The model you used does not use the Storage trait');
23
        }
24
25
        try {
26
            foreach ($model->images as $image) {
27
                $imageFiles = $this->getImages($model->filesDir(), $image->name, $model->getImageDimensions());
28
29
                foreach ($imageFiles as $imageFile) {
30
                    Storage::disk('s3')->put(Str::of($imageFile)->after('public/'), fopen($imageFile, 'r+'), 'public');
0 ignored issues
show
Bug introduced by
It seems like fopen($imageFile, 'r+') can also be of type false; however, parameter $contents of Illuminate\Filesystem\FilesystemAdapter::put() does only seem to accept resource|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
                    Storage::disk('s3')->put(Str::of($imageFile)->after('public/'), /** @scrutinizer ignore-type */ fopen($imageFile, 'r+'), 'public');
Loading history...
31
                }
32
            }
33
            $this->deleteFiles($model);
34
        } catch (\Exception $e) {
35
            Log::error($e->getMessage());
36
        }
37
    }
38
}
39