ProcessesImages   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 23
c 1
b 0
f 1
dl 0
loc 37
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B processImage() 0 27 6
1
<?php
2
3
namespace FaithGen\SDK\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use Intervention\Image\ImageManager;
7
use InvalidArgumentException;
8
9
trait ProcessesImages
10
{
11
    use FileTraits;
12
13
    /**
14
     * Creates thumbnails for the given model.
15
     *
16
     * @param ImageManager $imageManager
17
     * @param $model
18
     */
19
    protected function processImage(ImageManager $imageManager, $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
        if ($model->images()->exists()) {
26
            foreach ($model->images as $image) {
27
                $originalFile = $this->getImage($model->filesDir(), $image->name);
28
29
                $thumbNailsDimensions = collect($model->getImageDimensions())
30
                    ->except(0)
31
                    ->toArray();
32
33
                foreach ($thumbNailsDimensions as $thumbNailsDimension) {
34
                    try {
35
                        $thumbNail = $this->getImage($model->filesDir(), $image->name, $thumbNailsDimension);
36
                        $imageManager
37
                            ->make($originalFile)
38
                            ->fit($thumbNailsDimension, $thumbNailsDimension,
39
                                function ($constraint) {
40
                                    $constraint->upsize();
41
                                    $constraint->aspectRatio();
42
                                }, 'center')
43
                            ->save($thumbNail);
44
                    } catch (\Exception $e) {
45
                        Log::error($e->getMessage());
46
                    }
47
                }
48
            }
49
        }
50
    }
51
}
52