Passed
Push — main ( 86a6dd...ec1f24 )
by PRATIK
03:12
created

ImageRepository::calculateDimention()   B

Complexity

Conditions 10
Paths 6

Size

Total Lines 33
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 6
nop 2
dl 0
loc 33
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Adminetic\Website\Repositories;
4
5
use Adminetic\Website\Contracts\ImageRepositoryInterface;
6
use Adminetic\Website\Http\Requests\ImageRequest;
7
use Adminetic\Website\Models\Admin\Image;
8
use Illuminate\Support\Facades\Cache;
9
10
class ImageRepository implements ImageRepositoryInterface
11
{
12
    // Image Index
13
    public function indexImage()
14
    {
15
        $images = config('adminetic.caching', true)
16
            ? (Cache::has('images') ? Cache::get('images') : Cache::rememberForever('images', function () {
17
                return Image::latest()->get();
18
            }))
19
            : Image::latest()->get();
20
21
        return compact('images');
22
    }
23
24
    // Image Create
25
    public function createImage()
26
    {
27
        //
28
    }
29
30
    // Image Store
31
    public function storeImage(ImageRequest $request)
32
    {
33
        $image = Image::create($request->validated());
34
        $this->uploadImage($image, $request);
35
    }
36
37
    // Image Show
38
    public function showImage(Image $image)
39
    {
40
        return compact('image');
41
    }
42
43
    // Image Edit
44
    public function editImage(Image $image)
45
    {
46
        return compact('image');
47
    }
48
49
    // Image Update
50
    public function updateImage(ImageRequest $request, Image $image)
51
    {
52
        $image->update($request->validated());
53
    }
54
55
    // Image Destroy
56
    public function destroyImage(Image $image)
57
    {
58
        $image->hardDelete('image');
59
        $image->delete();
60
    }
61
62
    // Image Upload
63
    protected function uploadImage($image, $request)
64
    {
65
        if (request()->image) {
66
            $dimension = $this->calculateDimention($image, $request);
67
            $thumbnails = [
68
                'storage' => 'website/image/' . validImageFolder($image->type, 'image'),
0 ignored issues
show
Bug introduced by
The function validImageFolder was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

68
                'storage' => 'website/image/' . /** @scrutinizer ignore-call */ validImageFolder($image->type, 'image'),
Loading history...
69
                'width' => $dimension['width'],
70
                'height' => $dimension['height'],
71
                'quality' => '70',
72
                'thumbnails' => [
73
                    [
74
                        'thumbnail-name' => 'small',
75
                        'thumbnail-width' => $dimension['small-width'],
76
                        'thumbnail-height' => $dimension['small-height'],
77
                        'thumbnail-quality' => '30',
78
                    ],
79
                ],
80
            ];
81
            $image->makeThumbnail('image', $thumbnails);
82
        }
83
    }
84
85
    protected function calculateDimention($image, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $image is not used and could be removed. ( Ignorable by Annotation )

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

85
    protected function calculateDimention(/** @scrutinizer ignore-unused */ $image, $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        $dimention = [];
88
        if ($request->has('type')) {
89
            if ($request->type == 1 || $request->type == 'Normal') {
90
                $dimention['width'] = 600;
91
                $dimention['height'] = 600;
92
                $dimention['small-width'] = 100;
93
                $dimention['small-height'] = 100;
94
            } elseif ($request->type == 2 || $request->type == 'Horizontal') {
95
                $dimention['width'] = 800;
96
                $dimention['height'] = 600;
97
                $dimention['small-width'] = 150;
98
                $dimention['small-height'] = 100;
99
            } elseif ($request->type == 3 || $request->type == 'Vertical') {
100
                $dimention['width'] = 600;
101
                $dimention['height'] = 800;
102
                $dimention['small-width'] = 100;
103
                $dimention['small-height'] = 150;
104
            } elseif ($request->type == 4 || $request->type == 'Slider') {
105
                $dimention['width'] = 1920;
106
                $dimention['height'] = 1280;
107
                $dimention['small-width'] = 200;
108
                $dimention['small-height'] = 100;
109
            }
110
        } else {
111
            $dimention['width'] = 600;
112
            $dimention['height'] = 600;
113
            $dimention['small-width'] = 100;
114
            $dimention['small-height'] = 100;
115
        }
116
117
        return $dimention;
118
    }
119
}
120