Completed
Push — master ( 5cf086...a327c9 )
by Davide
06:57 queued 03:45
created

JumbotronImageController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Test Coverage

Coverage 65.38%

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 228
ccs 51
cts 78
cp 0.6538
rs 10
c 0
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 7 1
A create() 0 4 1
A store() 0 11 1
A update() 0 11 1
A edit() 0 5 1
A saveOnDb() 0 23 2
A uploadImageOnServer() 0 26 2
A show() 0 5 1
A index() 0 27 2
A getJumbotronHeightArray() 0 10 1
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\Facades\LaravelJumbotronImages;
10
use DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage;
11
12
class JumbotronImageController
13
{
14
    /**
15
     * Display the specified resource.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @return \Illuminate\Http\Response
19
     */
20 3
    public function index(Request $request)
21
    {
22 3
        $searchKeywords = $request->input('keywords');
23
        //$searchCategory = $request->input('category_id');
24 3
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
25
26 3
        if ($searchKeywords) {
27
            $jumbotronImages = JumbotronImage::
28
                                select('jumbotron_image_translations.jumbotron_image_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
29
                                ->join('jumbotron_image_translations', 'jumbotron_images.id', '=', 'jumbotron_image_translations.jumbotron_image_id')
30
                                ->orderBy('title')
31
                                ->where('title', 'like', '%'.$searchKeywords.'%')
32
                                ->where('locale', 'en')
33
                                ->paginate(20);
34
        } else {
35
            $jumbotronImages = JumbotronImage::
36 3
                                select('jumbotron_image_translations.jumbotron_image_id AS id', 'title', 'body', 'button_text', 'image_file_name', 'button_url', 'locale')
37 3
                                ->join('jumbotron_image_translations', 'jumbotron_images.id', '=', 'jumbotron_image_translations.jumbotron_image_id')
38 3
                                ->where('locale', 'en')
39 3
                                ->orderBy('title')
40 3
                                ->paginate(20);
41
        }
42
43 3
        return view('laravel-jumbotron-images::jumbotronImages.index', compact('jumbotronImages'))
44 3
                             ->with('i', (request()->input('page', 1) - 1) * 20)
45 3
                             ->with('searchKeywords', $searchKeywords)
46 3
                             ->with('countriesAvailableForTranslations', $countriesAvailableForTranslations);
47
    }
48
49
    /***************************************************************************/
50
51
    /**
52
     * Show the form for creating a new resource.
53
     *
54
     * @return \Illuminate\Http\Response
55
     */
56 1
    public function create()
57
    {
58 1
        return view('laravel-jumbotron-images::jumbotronImages.create')
59 1
                    ->with('jumbotronHeightArray', $this->getJumbotronHeightArray());
60
    }
61
62
    /***************************************************************************/
63
64
    /**
65
     * Store a newly created resource in storage.
66
     *
67
     * @param  \Illuminate\Http\Request  $request
68
     * @return \Illuminate\Http\Response
69
     */
70 1
    public function store(Request $request)
71
    {
72 1
        $jumbotronImage = new JumbotronImage();
73
74
        // Set the default language to edit the quote in English
75 1
        App::setLocale('en');
76
77 1
        $this->saveOnDb($request, $jumbotronImage);
78
79 1
        return redirect()->route('jumbotron-images.index')
80 1
                            ->with('success', 'Jumbotron image added succesfully');
81
    }
82
83
    /***************************************************************************/
84
85
    /**
86
     * Display the specified resource.
87
     *
88
     * @param  int $jumbotronImageId
89
     * @return \Illuminate\Http\Response
90
     */
91 1
    public function show($jumbotronImageId = null)
92
    {
93 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
94
95 1
        return view('laravel-jumbotron-images::jumbotronImages.show', compact('jumbotronImage'));
96
    }
97
98
    /***************************************************************************/
99
100
    /**
101
     * Show the form for editing the specified resource.
102
     *
103
     * @param  int $jumbotronImageId
104
     * @return \Illuminate\Http\Response
105
     */
106 1
    public function edit($jumbotronImageId = null)
107
    {
108 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
109
110 1
        return view('laravel-jumbotron-images::jumbotronImages.edit', compact('jumbotronImage'));
111
    }
112
113
    /***************************************************************************/
114
115
    /**
116
     * Update the specified resource in storage.
117
     *
118
     * @param  \Illuminate\Http\Request  $request
119
     * @param  int  $jumbotronImageId
120
     * @return \Illuminate\Http\Response
121
     */
122 1
    public function update(Request $request, $jumbotronImageId)
123
    {
124 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
125
126
        // Set the default language to update the quote in English
127 1
        App::setLocale('en');
128
129 1
        $this->saveOnDb($request, $jumbotronImage);
130
131 1
        return redirect()->route('jumbotron-images.index')
132 1
                            ->with('success', 'Jumbotron image updated succesfully');
133
    }
134
135
    /***************************************************************************/
136
137
    /**
138
     * Remove the specified resource from storage.
139
     *
140
     * @param  int  $jumbotronImageId
141
     * @return \Illuminate\Http\Response
142
     */
143 1
    public function destroy($jumbotronImageId)
144
    {
145 1
        $jumbotronImage = JumbotronImage::find($jumbotronImageId);
146 1
        $jumbotronImage->delete();
147
148 1
        return redirect()->route('jumbotron-images.index')
149 1
                            ->with('success', 'Jumbotron image deleted succesfully');
150
    }
151
152
    /***************************************************************************/
153
154
    /**
155
     * Save the record on DB.
156
     * @param  \Illuminate\Http\Request  $request
157
     * @param  \DavideCasiraghi\LaravelJumbotronImages\Models\JumbotronImage  $jumbotronImage
158
     * @return void
159
     */
160 2
    public function saveOnDb($request, $jumbotronImage)
161
    {
162 2
        $jumbotronImage->translateOrNew('en')->title = $request->get('title');
163 2
        $jumbotronImage->translateOrNew('en')->body = $request->get('body');
164 2
        $jumbotronImage->translateOrNew('en')->button_text = $request->get('button_text');
165
        //$jumbotronImage->image_file_name = $request->get('image_file_name');
166 2
        $jumbotronImage->button_url = $request->get('button_url');
167
168
        // Teacher profile picture upload
169 2
        if ($request->file('image_file_name')) {
170
            $imageFile = $request->file('image_file_name');
171
            $imageName = $imageFile->hashName();
172
            $imageSubdir = 'jumbotron_images';
173
            $imageWidth = '1067';
174
            $thumbWidth = '690';
175
176
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
177
            $jumbotronImage->image_file_name = $imageName;
178
        } else {
179 2
            $jumbotronImage->image_file_name = $request->image_file_name;
180
        }
181
182 2
        $jumbotronImage->save();
183 2
    }
184
185
    /***************************************************************************/
186
187
    /**
188
     * Upload image on server.
189
     *
190
     * @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...
191
     * @param  $imageName - the file name
192
     * @param  $imageSubdir - the subdir in /storage/app/public/images/..
193
     * @return void
194
     */
195
    public function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
196
    {
197
198
        // Create dir if not exist (in /storage/app/public/images/..)
199
        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

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