Completed
Push — master ( 633925...7a4edf )
by Davide
08:59
created

Controller::uploadImageOnServer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 5
crap 2
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
//use DavideCasiraghi\LaravelEventsCalendar\Models\User;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Foundation\Bus\DispatchesJobs;
9
use Intervention\Image\ImageManagerStatic as Image;
10
use Illuminate\Routing\Controller as BaseController;
11
use Illuminate\Foundation\Validation\ValidatesRequests;
12
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
13
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
14
15
class Controller extends BaseController
16
{
17
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
18
19
    // https://stackoverflow.com/questions/51611015/authuser-return-null-5-6
20
    public function __construct()
21
    {
22
        $this->middleware(function ($request, $next) {
23
            $this->user = Auth::user();
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
25
            return $next($request);
26
        });
27
    }
28
29
    // **********************************************************************
30
31
    /**
32
     * Get the current logged user ID.
33
     * If user is admin or super admin return 0.
34
     *
35
     * @return int $ret
36
     */
37
    public function getLoggedUser()
38
    {
39
        $user = Auth::user();
40
41
        // This is needed to not get error in the queries with: ->when($loggedUser->id, function ($query, $loggedUserId) {
42
        /*if (! $user) {
43
            $user = new User();
44
            $user->name = null;
45
            $user->group = null;
46
        }*/
47
48
        $ret = $user;
49
50
        return $ret;
51
    }
52
53
    // **********************************************************************
54
55
    /**
56
     * Get the current logged user id.
57
     *
58
     * @return bool $ret - the current logged user id, if admin or super admin 0
59
     */
60 17
    public function getLoggedAuthorId()
61
    {
62 17
        $user = Auth::user();
63
64 17
        $ret = null;
65
66 17
        if ($user) {
67
            //disabled for the tests errors -- still to solve the isSuperAdmin()
68
            //$ret = (! $user->isSuperAdmin() && ! $user->isAdmin()) ? $user->id : 0;
69 14
            $ret = (! $user->group == 1 && ! $user->group == 2) ? $user->id : 0;
0 ignored issues
show
Bug introduced by
Accessing group on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
70
        }
71
        /*if ($user) {
72
            $ret = $user->id;
73
        }*/
74
75 17
        return $ret;
76
    }
77
78
    // **********************************************************************
79
80
    /**
81
     * Upload image on server.
82
     * $imageFile - the file to upload
83
     * $imageSubdir is the subdir in /storage/app/public/images/..
84
     *
85
     * @param  array $imageFile
86
     * @param  string $imageName
87
     * @param  string $imageSubdir
88
     * @param  string $imageWidth
89
     * @param  string $thumbWidth
90
     * @return void
91
     */
92 1
    public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
93
    {
94
95
        // Create dir if not exist (in /storage/app/public/images/..)
96 1
        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

96
        if (! Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
97 1
            Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
98
        }
99
100 1
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
101
102
        // Resize the image with Intervention - http://image.intervention.io/api/resize
103
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
104
        // - save file as jpg with medium quality
105 1
        $image = Image::make($imageFile->getRealPath())
106 1
                                ->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

106
                                ->resize(/** @scrutinizer ignore-type */ $imageWidth, null,
Loading history...
107
                                    function ($constraint) {
108 1
                                        $constraint->aspectRatio();
109 1
                                    })
110 1
                                ->save(storage_path($destinationPath.$imageName), 75);
111
112
        // Create the thumb
113 1
        $image->resize($thumbWidth, null,
114
                    function ($constraint) {
115 1
                        $constraint->aspectRatio();
116 1
                    })
117 1
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
118 1
    }
119
120
    // **********************************************************************
121
122
    /**
123
     * Get the language name from language code.
124
     *
125
     * @param  string $languageCode
126
     * @return string
127
     */
128 2
    public function getSelectedLocaleName($languageCode)
129
    {
130 2
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
131 2
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
132
133 2
        return $ret;
134
    }
135
}
136