Passed
Push — master ( edb8f2...3ffc02 )
by PRATIK
02:11
created

Thumbnail::imageProperty()   C

Complexity

Conditions 13
Paths 97

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 19
c 1
b 0
f 0
nc 97
nop 1
dl 0
loc 24
rs 6.6166

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 drh2so4\Thumbnail\Traits;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\File;
7
use Intervention\Image\Facades\Image as Image;
8
9
trait Thumbnail
10
{
11
    public function makeThumbnail($fieldname = "image", $custom = [])
12
    {
13
14
        if (!empty(request()->$fieldname) && request()->has($fieldname) || $custom['image']) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! empty(request()->$fie...e)) || $custom['image'], Probably Intended Meaning: ! empty(request()->$fiel...e) || $custom['image'])
Loading history...
15
16
            /* ------------------------------------------------------------------- */
17
18
            $image_file = $custom['image'] ?? request()->file($fieldname); // Retriving Image File
19
            $filenamewithextension  = $image_file->getClientOriginalName(); //Retriving Full Image Name
0 ignored issues
show
Bug introduced by
The method getClientOriginalName() does not exist on null. ( Ignorable by Annotation )

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

19
            /** @scrutinizer ignore-call */ 
20
            $filenamewithextension  = $image_file->getClientOriginalName(); //Retriving Full Image Name

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
20
            $raw_filename = pathinfo($filenamewithextension, PATHINFO_FILENAME); //Retriving Image Raw Filename only
21
            $filename = str_replace('-', '', $raw_filename); // Retrive Filename
22
            $extension = $image_file->getClientOriginalExtension(); //Retriving Image extension
23
            $imageStoreNameOnly = $filename . "-" . time(); //Making Image Store name
24
            $imageStoreName = $filename . "-" . time() . "." . $extension; //Making Image Store name
25
26
            /* ------------------------------------------------------------------- */
27
28
            /* ----------------------------------------Image Upload----------------------------------------- */
29
            $img = $custom['image'] ?? request()->$fieldname;
30
            $this->update([
0 ignored issues
show
Bug introduced by
It seems like update() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   update([
Loading history...
31
                $fieldname => $img->storeAs($custom['storage'] ?? config("thumbnail.storage_path", "uploads"), $imageStoreName, 'public')
32
            ]);
33
            /* --------------------------------------------------------------------------------------------- */
34
35
            $image = Image::cache(function ($cached_img) use ($image_file, $custom) {
36
                return $cached_img->make($image_file->getRealPath())->fit($custom['width'] ?? config('thumbnail.img_width', 1000), $custom['height'] ?? config('thumbnail.img_height', 800));
37
            }, config('thumbnail.image_cached_time', 10), true);
38
            $image->save(public_path('storage/' . $this->$fieldname), $custom['quality'] ?? config('thumbnail.image_quality', 80));
39
40
            if (config('thumbnail.thumbnail', true)) {
41
                $thumbnails = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $thumbnails is dead and can be removed.
Loading history...
42
                $thumbnails = $custom['thumbnails'] ?? config('thumbnail.thumbnails', false) ?? false;
43
                $storage = $custom['storage'] ?? config('thumbnail.thumbnails_storage', false) ?? false;
44
                if ($thumbnails) {
45
                    /* --------------------------------Custom Thumbnails------------------------------------------------- */
46
                    foreach ($thumbnails as $thumbnail) {
47
                        $customthumbnail = $imageStoreNameOnly  . '-' . $thumbnail['thumbnail-name'] . '.' . $extension; // Making Thumbnail Name
48
                        $custom_thumbnail = $image_file->storeAs($storage ?? config("thumbnail.storage_path", "uploads"), $customthumbnail, 'public'); // Thumbnail Storage Information
49
                        $make_custom_thumbnail = Image::cache(function ($cached_img) use ($image_file, $thumbnail) {
50
                            return $cached_img->make($image_file->getRealPath())->fit($thumbnail['thumbnail-width'], $thumbnail['thumbnail-height']);
51
                        }, config('thumbnail.image_cached_time', 10), true); //Storing Thumbnail
52
                        $make_custom_thumbnail->save(public_path('storage/' . $custom_thumbnail), $thumbnail['thumbnail-quality']); //Storing Thumbnail 
0 ignored issues
show
Bug introduced by
Are you sure $custom_thumbnail of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

52
                        $make_custom_thumbnail->save(public_path('storage/' . /** @scrutinizer ignore-type */ $custom_thumbnail), $thumbnail['thumbnail-quality']); //Storing Thumbnail 
Loading history...
53
                    }
54
                    /* -------------------------------------------------------------------------------------------------- */
55
                } else {
56
                    /* --------------------- Thumbnail Info--------------------------------- */
57
                    //small thumbnail name
58
                    $smallthumbnail =  $imageStoreNameOnly  . '-small' . '.' . $extension; // Making Thumbnail Name
59
60
                    //medium thumbnail name
61
                    $mediumthumbnail =  $imageStoreNameOnly  . '-medium' . '.' . $extension; // Making Thumbnail Name
62
63
                    $small_thumbnail = $image_file->storeAs(config("thumbnail.storage_path", "uploads"), $smallthumbnail, 'public'); // Thumbnail Storage Information
64
                    $medium_thumbnail = $image_file->storeAs(config("thumbnail.storage_path", "uploads"), $mediumthumbnail, 'public'); // Thumbnail Storage Information
65
66
                    /* --------------------------------- Saving Thumbnail------------------------------------ */
67
68
                    $medium_img = Image::cache(function ($cached_img) use ($image_file) {
69
                        return $cached_img->make($image_file->getRealPath())->fit(config('thumbnail.medium_thumbnail_width', 800), config('thumbnail.medium_thumbnail_height', 600)); //Storing Thumbnail
70
                    }, config('thumbnail.image_cached_time', 10), true);
71
72
                    $medium_img->save(public_path('storage/' . $medium_thumbnail), config('thumbnail.medium_thumbnail_quality', 60)); //Storing Thumbnail
0 ignored issues
show
Bug introduced by
Are you sure $medium_thumbnail of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

72
                    $medium_img->save(public_path('storage/' . /** @scrutinizer ignore-type */ $medium_thumbnail), config('thumbnail.medium_thumbnail_quality', 60)); //Storing Thumbnail
Loading history...
73
74
                    $small_img = Image::cache(function ($cached_img) use ($image_file) {
75
                        return $cached_img->make($image_file->getRealPath())->fit(config('thumbnail.small_thumbnail_width', 400), config('thumbnail.small_thumbnail_height', 300)); //Storing Thumbnail
76
                    }, config('thumbnail.image_cached_time', 10), true);
77
78
                    $small_img->save(public_path('storage/' . $small_thumbnail), config('thumbnail.small_thumbnail_quality', 30)); //Storing Thumbnail
0 ignored issues
show
Bug introduced by
Are you sure $small_thumbnail of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

78
                    $small_img->save(public_path('storage/' . /** @scrutinizer ignore-type */ $small_thumbnail), config('thumbnail.small_thumbnail_quality', 30)); //Storing Thumbnail
Loading history...
79
80
                    /* ------------------------------------------------------------------------------------- */
81
                }
82
            }
83
        }
84
    }
85
86
    // Thumbnail Return
87
    public function thumbnail($fieldname = "image", $size)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldname 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

87
    public function thumbnail(/** @scrutinizer ignore-unused */ $fieldname = "image", $size)

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...
88
    {
89
        return $this->imageDetail($fieldname = "image", $size)->path;
90
    }
91
92
    /* Image Details */
93
    public function imageDetail($fieldname = "image", $size = null)
94
    {
95
        $image = $this->$fieldname;
96
        $path = explode("/", $image);
97
        $extension = \File::extension($image);
98
        $name = basename($image, "." . $extension);
99
        $image_fullname = isset($size) ? $name . "-" . (string) $size . "." . $extension : $name . "." . $extension;
100
        array_pop($path);
101
        $location = implode("/", $path);
102
        $path = "storage/" . $location . "/" . $image_fullname;
103
        $image_files = File::files(public_path('storage/' . $location));
104
        $images_property = $this->imageProperty($image_files);
105
        $image_detail = array(
106
            'image' => $image,
107
            'name' => $name,
108
            'fullname' => $image_fullname,
109
            'extension' => $extension,
110
            'path' => $path,
111
            'directory' => public_path('storage/' . $location),
112
            'location' => public_path('storage/' . $image),
113
            'property' => $images_property
114
        );
115
116
        return json_decode(json_encode($image_detail));
117
    }
118
119
120
    /* Checking Image Existance */
121
    public function imageExists($image)
122
    {
123
        return file_exists(public_path('storage/' . $image)) ? true : false;
124
    }
125
126
    // Checking Image's Thumbnail Existance
127
    public function hasThumbnail($image)
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

127
    public function hasThumbnail(/** @scrutinizer ignore-unused */ $image)

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...
128
    {
129
        //
130
    }
131
132
    // Image Property
133
    private function imageProperty($image_files)
134
    {
135
        $images_property = array();
136
        $thumbnails_property = array();
137
        foreach ($image_files as $image) {
138
            $image_partition = explode('-', basename($image));
139
            if (isset($image_partition[2])) {
140
                $thumbnails_property['image'] = $image->getFilename() ?? null;
141
                $thumbnails_property['real_name'] = isset($image_partition[0]) ? $image_partition[0] : null;
142
                $thumbnails_property['size'] = isset($image_partition[0]) ? $image->getSize() : null;
143
                $thumbnails_property['created_date'] = isset($image_partition[1]) ? Carbon::createFromFormat('Y/m/d H:i:s', date('Y/m/d H:i:s', $image_partition[1])) : null;
0 ignored issues
show
Bug introduced by
$image_partition[1] of type string is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

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

143
                $thumbnails_property['created_date'] = isset($image_partition[1]) ? Carbon::createFromFormat('Y/m/d H:i:s', date('Y/m/d H:i:s', /** @scrutinizer ignore-type */ $image_partition[1])) : null;
Loading history...
144
                $thumbnails_property['directory'] = isset($image_partition[0]) ? $image->getPath() : null;
145
                $thumbnails_property['location'] = isset($image_partition[0]) ? $image->getRealPath() : null;
146
            } else {
147
                $images_property['real_name'] = isset($image_partition[0]) ? $image_partition[0] : null;
148
                $images_property['size'] = isset($image_partition[0]) ? $image->getSize() : null;
149
                $images_property['directory'] = isset($image_partition[0]) ? $image->getPath() : null;
150
                $images_property['location'] = isset($image_partition[0]) ? $image->getRealPath() : null;
151
            }
152
            if (isset($image_partition[2])) {
153
                $images_property['thumbnails'] = $thumbnails_property;
154
            }
155
        }
156
        return $images_property;
157
    }
158
}
159