Completed
Push — master ( 2d57a6...123316 )
by Davide
05:53
created

Controller::getSelectedLocaleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelSmartBlog\Http\Controllers;
4
5
//use App\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
    // **********************************************************************
20
21
    /**
22
     * Get the current logged user ID.
23
     * If user is admin or super admin return 0.
24
     *
25
     * @return int $ret
26
     */
27
    public function getLoggedUser()
28
    {
29
        $ret = Auth::user();
30
31
        return $ret;
32
    }
33
34
    // **********************************************************************
35
36
    /**
37
     * Get the current logged user id.
38
     *
39
     * @return bool $ret - the current logged user id, if admin($user->group == 2) or super admin()$user->group == 1) is stet to 0
40
     */
41
    public function getLoggedAuthorId()
42
    {
43
        $user = Auth::user();
44
        $ret = null;
45
        if ($user) {
46
            //$ret = (! $user->isSuperAdmin() && ! $user->isAdmin()) ? $user->id : 0;
47
            $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...
48
        }
49
50
        return $ret;
51
    }
52
53
    // **********************************************************************
54
55
    /**
56
     * Upload image on server.
57
     * $imageFile - the file to upload
58
     * $imageSubdir is the subdir in /storage/app/public/images/..
59
     *
60
     * @param  array $imageFile
61
     * @param  string $imageName
62
     * @param  string $imageSubdir
63
     * @param  string $imageWidth
64
     * @param  string $thumbWidth
65
     * @return void
66
     */
67 1
    public static function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
68
    {
69
70
        // Create dir if not exist (in /storage/app/public/images/..)
71 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

71
        if (! Storage::disk('public')->/** @scrutinizer ignore-call */ has('images/'.$imageSubdir.'/')) {
Loading history...
72 1
            Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
73
        }
74
75 1
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
76
77
        // Resize the image with Intervention - http://image.intervention.io/api/resize
78
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
79
        // - save file as jpg with medium quality
80 1
        $image = Image::make($imageFile->getRealPath())
81 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

81
                                ->resize(/** @scrutinizer ignore-type */ $imageWidth, null,
Loading history...
82
                                    function ($constraint) {
83 1
                                        $constraint->aspectRatio();
84 1
                                    })
85 1
                                ->save(storage_path($destinationPath.$imageName), 75);
86
87
        // Create the thumb
88 1
        $image->resize($thumbWidth, null,
89
                    function ($constraint) {
90 1
                        $constraint->aspectRatio();
91 1
                    })
92 1
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
93 1
    }
94
95
    // **********************************************************************
96
97
    /**
98
     * Get the language name from language code.
99
     *
100
     * @param  string $languageCode
101
     * @return string
102
     */
103 6
    public function getSelectedLocaleName($languageCode)
104
    {
105 6
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
106 6
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
107
108 6
        return $ret;
109
    }
110
}
111