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

197
        if (! \Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
198
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
199
        }
200
201
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
202
203
        // Resize the image with Intervention - http://image.intervention.io/api/resize
204
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
205
        // - save file as jpg with medium quality
206
        $image = Image::make($imageFile->getRealPath())
207
                                ->resize($imageWidth, null,
208
                                    function ($constraint) {
209
                                        $constraint->aspectRatio();
210
                                    })
211
                                ->save(storage_path($destinationPath.$imageName), 75);
212
213
        // Create the thumb
214
        $image->resize($thumbWidth, null,
215
                    function ($constraint) {
216
                        $constraint->aspectRatio();
217
                    })
218
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
219
    }
220
221
    // **********************************************************************
222
}
223