Completed
Push — master ( 95abe7...6e7824 )
by Davide
03:14
created

JumbotronImageController::saveOnDb()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 30
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.7044

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 16
nop 2
dl 0
loc 30
ccs 16
cts 23
cp 0.6957
crap 5.7044
rs 9.2408
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelJumbotronImages\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\App;
7
use Intervention\Image\ImageManagerStatic as Image;
8
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
9
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage;
10
11
class JumbotronImageController
12
{
13
    /**
14
     * Display the specified resource.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @return \Illuminate\Http\Response
18
     */
19 3
    public function index(Request $request)
20
    {
21 3
        $searchKeywords = $request->input('keywords');
22
        //$searchCategory = $request->input('category_id');
23 3
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
24
25 3
        if ($searchKeywords) {
26
            $jumbotronImages = JumbotronImage::
27
                                select('jumbotron_image_translations.jumbotron_image_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
28
                                ->join('jumbotron_image_translations', 'jumbotron_images.id', '=', 'jumbotron_image_translations.jumbotron_image_id')
29
                                ->orderBy('title')
30
                                ->where('title', 'like', '%'.$searchKeywords.'%')
31
                                ->where('locale', 'en')
32
                                ->paginate(20);
33
        } else {
34
            $jumbotronImages = JumbotronImage::
35 3
                                select('jumbotron_image_translations.jumbotron_image_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
36 3
                                ->join('jumbotron_image_translations', 'jumbotron_images.id', '=', 'jumbotron_image_translations.jumbotron_image_id')
37 3
                                ->where('locale', 'en')
38 3
                                ->orderBy('title')
39 3
                                ->paginate(20);
40
        }
41
42 3
        return view('laravel-jumbotron-images::jumbotronImages.index', compact('jumbotronImages'))
43 3
                             ->with('i', (request()->input('page', 1) - 1) * 20)
44 3
                             ->with('searchKeywords', $searchKeywords)
45 3
                             ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
46
    }
47
48
    /***************************************************************************/
49
50
    /**
51
     * Show the form for creating a new resource.
52
     *
53
     * @return \Illuminate\Http\Response
54
     */
55 1
    public function create()
56
    {
57 1
        return view('laravel-jumbotron-images::jumbotronImages.create')
58 1
                    ->with('jumbotronHeightArray', $this->getJumbotronHeightArray())
59 1
                    ->with('buttonColorArray', $this->getButtonColorArray())
60 1
                    ->with('coverOpacityArray', $this->getCoverOpacityArray());
61
    }
62
63
    /***************************************************************************/
64
65
    /**
66
     * Store a newly created resource in storage.
67
     *
68
     * @param  \Illuminate\Http\Request  $request
69
     * @return \Illuminate\Http\Response
70
     */
71 1
    public function store(Request $request)
72
    {
73 1
        $jumbotronImage = new JumbotronImage();
74
75
        // Set the default language to edit the quote in English
76 1
        App::setLocale('en');
77
78 1
        $this->saveOnDb($request, $jumbotronImage);
79
80 1
        return redirect()->route('jumbotron-images.index')
81 1
                            ->with('success', 'Jumbotron image added succesfully');
82
    }
83
84
    /***************************************************************************/
85
86
    /**
87
     * Display the specified resource.
88
     *
89
     * @param  int $jumbotronImageId
90
     * @return \Illuminate\Http\Response
91
     */
92 1
    public function show($jumbotronImageId = null)
93
    {
94 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
95
96 1
        return view('laravel-jumbotron-images::jumbotronImages.show', compact('jumbotronImage'));
97
    }
98
99
    /***************************************************************************/
100
101
    /**
102
     * Show the form for editing the specified resource.
103
     *
104
     * @param  int $jumbotronImageId
105
     * @return \Illuminate\Http\Response
106
     */
107 1
    public function edit($jumbotronImageId = null)
108
    {
109 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
110
111 1
        return view('laravel-jumbotron-images::jumbotronImages.edit', compact('jumbotronImage'))
112 1
                    ->with('jumbotronHeightArray', $this->getJumbotronHeightArray())
113 1
                    ->with('buttonColorArray', $this->getButtonColorArray())
114 1
                    ->with('coverOpacityArray', $this->getCoverOpacityArray());
115
    }
116
117
    /***************************************************************************/
118
119
    /**
120
     * Update the specified resource in storage.
121
     *
122
     * @param  \Illuminate\Http\Request  $request
123
     * @param  int  $jumbotronImageId
124
     * @return \Illuminate\Http\Response
125
     */
126 1
    public function update(Request $request, $jumbotronImageId)
127
    {
128 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
129
130
        // Set the default language to update the quote in English
131 1
        App::setLocale('en');
132
133 1
        $this->saveOnDb($request, $jumbotronImage);
134
135 1
        return redirect()->route('jumbotron-images.index')
136 1
                            ->with('success', 'Jumbotron image updated succesfully');
137
    }
138
139
    /***************************************************************************/
140
141
    /**
142
     * Remove the specified resource from storage.
143
     *
144
     * @param  int  $jumbotronImageId
145
     * @return \Illuminate\Http\Response
146
     */
147 1
    public function destroy($jumbotronImageId)
148
    {
149 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
150 1
        $jumbotronImage->delete();
151
152 1
        return redirect()->route('jumbotron-images.index')
153 1
                            ->with('success', 'Jumbotron image deleted succesfully');
154
    }
155
156
    /***************************************************************************/
157
158
    /**
159
     * Save the record on DB.
160
     * @param  \Illuminate\Http\Request  $request
161
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage  $jumbotronImage
162
     * @return void
163
     */
164 2
    public function saveOnDb($request, $jumbotronImage)
165
    {
166 2
        $jumbotronImage->translateOrNew('en')->title = $request->get('title');
167 2
        $jumbotronImage->translateOrNew('en')->body = $request->get('body');
168 2
        $jumbotronImage->translateOrNew('en')->button_text = $request->get('button_text');
169 2
        $jumbotronImage->button_url = $request->get('button_url');
170 2
        $jumbotronImage->jumbotron_height = $request->get('jumbotron_height');
171 2
        $jumbotronImage->cover_opacity = $request->get('cover_opacity');
172 2
        $jumbotronImage->scroll_down_arrow = ($request->scroll_down_arrow == 'on') ? 1 : 0;
173 2
        $jumbotronImage->background_color = $request->get('background_color');
174 2
        $jumbotronImage->cover_opacity = $request->get('cover_opacity');
175 2
        $jumbotronImage->button_color = $request->get('button_color');
176 2
        $jumbotronImage->parallax = ($request->parallax == 'on') ? 1 : 0;
177 2
        $jumbotronImage->white_moon = ($request->white_moon == 'on') ? 1 : 0;
178
        
179
        // Teacher profile picture upload
180 2
        if ($request->file('image_file_name')) {
181
            $imageFile = $request->file('image_file_name');
182
            $imageName = $imageFile->hashName();
183
            $imageSubdir = 'jumbotron_images';
184
            $imageWidth = '1067';
185
            $thumbWidth = '690';
186
187
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
0 ignored issues
show
Bug introduced by
It seems like $imageFile can also be of type Illuminate\Http\UploadedFile; however, parameter $imageFile of DavideCasiraghi\LaravelJ...::uploadImageOnServer() does only seem to accept Illuminate\Http\UploadedFile[], 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

187
            $this->uploadImageOnServer(/** @scrutinizer ignore-type */ $imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
Loading history...
188
            $jumbotronImage->image_file_name = $imageName;
189
        } else {
190 2
            $jumbotronImage->image_file_name = $request->image_file_name;
191
        }
192
193 2
        $jumbotronImage->save();
194 2
    }
195
196
    /***************************************************************************/
197
198
    /**
199
     * Upload image on server.
200
     * $imageFile - the file to upload
201
     * $imageSubdir is the subdir in /storage/app/public/images/..
202
     *
203
     * @param  \Illuminate\Http\UploadedFile[] $imageFile
204
     * @param  string $imageName
205
     * @param  string $imageSubdir
206
     * @param  string $imageWidth
207
     * @param  string $thumbWidth
208
     * @return void
209
     */
210
    public function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
211
    {
212
213
        // Create dir if not exist (in /storage/app/public/images/..)
214
        if (! \Storage::disk('public')->has('images/'.$imageSubdir.'/')) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Contracts\Filesystem\Cloud. Are you sure you never get one of those? ( Ignorable by Annotation )

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

214
        if (! \Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
215
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
216
        }
217
218
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
219
220
        // Resize the image with Intervention - http://image.intervention.io/api/resize
221
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
222
        // - save file as jpg with medium quality
223
        $image = Image::make($imageFile->getRealPath())
224
                                ->resize($imageWidth, null,
0 ignored issues
show
Bug introduced by
$imageWidth of type string is incompatible with the type integer expected by parameter $width of Intervention\Image\Image::resize(). ( Ignorable by Annotation )

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

224
                                ->resize(/** @scrutinizer ignore-type */ $imageWidth, null,
Loading history...
225
                                    function ($constraint) {
226
                                        $constraint->aspectRatio();
227
                                    })
228
                                ->save(storage_path($destinationPath.$imageName), 75);
229
230
        // Create the thumb
231
        $image->resize($thumbWidth, null,
232
                    function ($constraint) {
233
                        $constraint->aspectRatio();
234
                    })
235
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
236
    }
237
238
    /***************************************************************************/
239
240
    /**
241
     * Return and array with the jumbotron possible height options.
242
     *
243
     * @return array
244
     */
245 2
    public static function getJumbotronHeightArray()
246
    {
247
        $ret = [
248 2
                 'is-small' => 'Small',
249
                 'is-medium' => 'Medium',
250
                 'is-large' => 'Large',
251
                 'is-halfheight' => 'Halfheight',
252
                 'is-fullheight' => 'Fullheight',
253
             ];
254
255 2
        return $ret;
256
    }
257
258
    /***************************************************************************/
259
260
    /**
261
     * Return and array with the jumbotron possible opacity options.
262
     *
263
     * @return array
264
     */
265 2
    public static function getCoverOpacityArray()
266
    {
267
        $ret = [
268 2
                 '0' => 'none',
269
                 '0.1' => '10%',
270
                 '0.2' => '20%',
271
                 '0.3' => '30%',
272
                 '0.4' => '40%',
273
                 '0.5' => '50%',
274
             ];
275
276 2
        return $ret;
277
    }
278
    
279
    /***************************************************************************/
280
281
    /**
282
     * Return and array with the button possible color options.
283
     *
284
     * @return array
285
     */
286 2
    public static function getButtonColorArray()
287
    {
288
        $ret = [
289 2
                 'press-red' => 'Red',
290
                 'press-pink' => 'Pink',
291
                 'press-purple' => 'Purple',
292
                 'press-deeppurple' => 'Deep purple',
293
                 'press-indigo' => 'Indigo',
294
                 'press-blue' => 'Blue',
295
                 'press-lightblue' => 'Light blue',
296
                 'press-cyan' => 'Cyan',
297
                 'press-teal' => 'Teal',
298
                 'press-green' => 'Green',
299
                 'press-lightgreen' => 'Light green',
300
                 'press-lime' => 'Lime',
301
                 'press-yellow' => 'Yellow',
302
                 'press-amber' => 'Amber',
303
                 'press-orange' => 'Orange',
304
                 'press-deeporange' => 'Deeporange',
305
                 'press-brown' => 'Brown',
306
                 'press-grey' => 'Grey',
307
                 'press-bluegrey' => 'Blue grey',
308
                 'press-black' => 'Black',
309
             ];
310
311 2
        return $ret;
312
    }
313
314
    /***************************************************************************/
315
}
316