Passed
Push — master ( eab33a...93cd42 )
by Innocent
04:10
created

ProcessesImages::processImage()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 18
c 1
b 0
f 1
nc 7
nop 2
dl 0
loc 25
rs 9.0444
1
<?php
2
3
namespace FaithGen\SDK\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use Intervention\Image\ImageManager;
7
8
trait ProcessesImages
9
{
10
    use FileTraits;
11
12
    /**
13
     * Creates thumbnails for the given model.
14
     *
15
     * @param ImageManager $imageManager
16
     * @param $model
17
     */
18
    protected function processImage(ImageManager $imageManager, $model)
19
    {
20
        if (! in_array(StorageTrait::class, class_uses($model))) {
21
            throw new InvalidArgumentException('The model you used does not use the Storage trait');
0 ignored issues
show
Bug introduced by
The type FaithGen\SDK\Traits\InvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
22
        }
23
24
        if ($model->images()->exists()) {
25
            foreach ($model->images as $image) {
26
                $originalFile = $this->getImage($model->filesDir(), $image->name);
27
28
                $thumbNailsDimensions = collect($model->getImageDimensions())
29
                    ->except(0)
30
                    ->toArray();
31
32
                foreach ($thumbNailsDimensions as $thumbNailsDimension) {
33
34
                    try {
35
                        $thumbNail = $this->getImage($model->filesDir(), $image->name, $thumbNailsDimension);
36
                        $imageManager->make($originalFile)->fit($thumbNailsDimension, $thumbNailsDimension,
37
                            function ($constraint) {
38
                                $constraint->upsize();
39
                                $constraint->aspectRatio();
40
                            }, 'center')->save($thumbNail);
41
                    } catch (\Exception $e) {
42
                        Log::error($e->getMessage());
43
                    }
44
                }
45
            }
46
        }
47
    }
48
}
49