Completed
Pull Request — master (#10)
by Davide
03:35
created

JumbotronImageController::uploadImageOnServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 5
dl 0
loc 26
ccs 0
cts 13
cp 0
crap 6
rs 9.8666
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
    }
60
61
    /***************************************************************************/
62
63
    /**
64
     * Store a newly created resource in storage.
65
     *
66
     * @param  \Illuminate\Http\Request  $request
67
     * @return \Illuminate\Http\Response
68
     */
69 1
    public function store(Request $request)
70
    {
71 1
        $jumbotronImage = new JumbotronImage();
72
73
        // Set the default language to edit the quote in English
74 1
        App::setLocale('en');
75
76 1
        $this->saveOnDb($request, $jumbotronImage);
77
78 1
        return redirect()->route('jumbotron-images.index')
79 1
                            ->with('success', 'Jumbotron image added succesfully');
80
    }
81
82
    /***************************************************************************/
83
84
    /**
85
     * Display the specified resource.
86
     *
87
     * @param  int $jumbotronImageId
88
     * @return \Illuminate\Http\Response
89
     */
90 1
    public function show($jumbotronImageId = null)
91
    {
92 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
93
94 1
        return view('laravel-jumbotron-images::jumbotronImages.show', compact('jumbotronImage'));
95
    }
96
97
    /***************************************************************************/
98
99
    /**
100
     * Show the form for editing the specified resource.
101
     *
102
     * @param  int $jumbotronImageId
103
     * @return \Illuminate\Http\Response
104
     */
105 1
    public function edit($jumbotronImageId = null)
106
    {
107 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
108
109 1
        return view('laravel-jumbotron-images::jumbotronImages.edit', compact('jumbotronImage'));
110
    }
111
112
    /***************************************************************************/
113
114
    /**
115
     * Update the specified resource in storage.
116
     *
117
     * @param  \Illuminate\Http\Request  $request
118
     * @param  int  $jumbotronImageId
119
     * @return \Illuminate\Http\Response
120
     */
121 1
    public function update(Request $request, $jumbotronImageId)
122
    {
123 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
124
125
        // Set the default language to update the quote in English
126 1
        App::setLocale('en');
127
128 1
        $this->saveOnDb($request, $jumbotronImage);
129
130 1
        return redirect()->route('jumbotron-images.index')
131 1
                            ->with('success', 'Jumbotron image updated succesfully');
132
    }
133
134
    /***************************************************************************/
135
136
    /**
137
     * Remove the specified resource from storage.
138
     *
139
     * @param  int  $jumbotronImageId
140
     * @return \Illuminate\Http\Response
141
     */
142 1
    public function destroy($jumbotronImageId)
143
    {
144 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
145 1
        $jumbotronImage->delete();
146
147 1
        return redirect()->route('jumbotron-images.index')
148 1
                            ->with('success', 'Jumbotron image deleted succesfully');
149
    }
150
151
    /***************************************************************************/
152
153
    /**
154
     * Save the record on DB.
155
     * @param  \Illuminate\Http\Request  $request
156
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage  $jumbotronImage
157
     * @return void
158
     */
159 2
    public function saveOnDb($request, $jumbotronImage)
160
    {
161 2
        $jumbotronImage->translateOrNew('en')->title = $request->get('title');
162 2
        $jumbotronImage->translateOrNew('en')->body = $request->get('body');
163 2
        $jumbotronImage->translateOrNew('en')->button_text = $request->get('button_text');
164
        //$jumbotronImage->image_file_name = $request->get('image_file_name');
165 2
        $jumbotronImage->button_url = $request->get('button_url');
166
167
        // Teacher profile picture upload
168 2
        if ($request->file('image_file_name')) {
169
            $imageFile = $request->file('image_file_name');
170
            $imageName = $imageFile->hashName();
171
            $imageSubdir = 'jumbotron_images';
172
            $imageWidth = '1067';
173
            $thumbWidth = '690';
174
175
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
176
            $jumbotronImage->image_file_name = $imageName;
177
        } else {
178 2
            $jumbotronImage->image_file_name = $request->image_file_name;
179
        }
180
181 2
        $jumbotronImage->save();
182 2
    }
183
184
    /***************************************************************************/
185
186
    /**
187
     * Upload image on server.
188
     *
189
     * @param  $imageFile - the file to upload
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
190
     * @param  $imageName - the file name
191
     * @param  $imageSubdir - the subdir in /storage/app/public/images/..
192
     * @return void
193
     */
194
    public function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
195
    {
196
197
        // Create dir if not exist (in /storage/app/public/images/..)
198
        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

198
        if (! \Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
199
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
200
        }
201
202
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
203
204
        // Resize the image with Intervention - http://image.intervention.io/api/resize
205
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
206
        // - save file as jpg with medium quality
207
        $image = Image::make($imageFile->getRealPath())
208
                                ->resize($imageWidth, null,
209
                                    function ($constraint) {
210
                                        $constraint->aspectRatio();
211
                                    })
212
                                ->save(storage_path($destinationPath.$imageName), 75);
213
214
        // Create the thumb
215
        $image->resize($thumbWidth, null,
216
                    function ($constraint) {
217
                        $constraint->aspectRatio();
218
                    })
219
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
220
    }
221
222
    /***************************************************************************/
223
224
    /**
225
     * Return the gift kind array.
226
     *
227
     * @return array
228
     */
229 1
    public static function getJumbotronHeightArray()
230
    {
231
        $ret = [
232 1
                 'is-small' => 'Small',
233
                 'is-medium' => 'Medium',
234
                 'is-large' => 'Large',
235
                 'is-halfheight' => 'Halfheight',
236
                 'is-fullheight' => 'Fullheight',
237
             ];
238
239 1
        return $ret;
240
    }
241
242
    /***************************************************************************/
243
}
244