Completed
Push — master ( ba992d...dc755b )
by Davide
07:03
created

Controller   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 7
dl 0
loc 101
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getLoggedUser() 0 15 2
A getLoggedAuthorId() 0 11 4
A uploadImageOnServer() 0 27 2
A getSelectedLocaleName() 0 7 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\User;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Foundation\Bus\DispatchesJobs;
8
use Illuminate\Routing\Controller as BaseController;
9
use Illuminate\Foundation\Validation\ValidatesRequests;
10
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
11
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
12
13
class Controller extends BaseController
14
{
15
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
16
17
    // **********************************************************************
18
19
    /**
20
     * Get the current logged user ID.
21
     * If user is admin or super admin return 0.
22
     *
23
     * @return int $ret
24
     */
25
    public function getLoggedUser()
26
    {
27
        $user = Auth::user();
28
29
        // This is needed to not get error in the queries with: ->when($loggedUser->id, function ($query, $loggedUserId) {
30
        if (! $user) {
31
            $user = new User();
32
            $user->name = null;
33
            $user->group = null;
34
        }
35
36
        $ret = $user;
37
38
        return $ret;
39
    }
40
41
    // **********************************************************************
42
43
    /**
44
     * Get the current logged user id.
45
     *
46
     * @return bool $ret - the current logged user id, if admin or super admin 0
47
     */
48
    public function getLoggedAuthorId()
49
    {
50
        $user = Auth::user();
51
52
        $ret = null;
53
        if ($user) {
54
            $ret = (! $user->isSuperAdmin() && ! $user->isAdmin()) ? $user->id : 0;
0 ignored issues
show
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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The method isSuperAdmin() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method isAdmin() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        }
56
57
        return $ret;
58
    }
59
60
    // **********************************************************************
61
62
    /**
63
     * Upload image on server.
64
     *
65
     * @param  $imageFile - the file to upload
66
     * @param  $imageName - the file name
67
     * @param  $imageSubdir - the subdir in /storage/app/public/images/..
68
     * @return void
69
     */
70
    public function uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth)
71
    {
72
73
        // Create dir if not exist (in /storage/app/public/images/..)
74
        if (! \Storage::disk('public')->has('images/'.$imageSubdir.'/')) {
0 ignored issues
show
Bug introduced by
The method has() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
            \Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/');
76
        }
77
78
        $destinationPath = 'app/public/images/'.$imageSubdir.'/';
79
80
        // Resize the image with Intervention - http://image.intervention.io/api/resize
81
        // -  resize and store the image to a width of 300 and constrain aspect ratio (auto height)
82
        // - save file as jpg with medium quality
83
        $image = \Image::make($imageFile->getRealPath())
84
                                ->resize($imageWidth, null,
85
                                    function ($constraint) {
86
                                        $constraint->aspectRatio();
87
                                    })
88
                                ->save(storage_path($destinationPath.$imageName), 75);
89
90
        // Create the thumb
91
        $image->resize($thumbWidth, null,
92
                    function ($constraint) {
93
                        $constraint->aspectRatio();
94
                    })
95
                ->save(storage_path($destinationPath.'thumb_'.$imageName), 75);
96
    }
97
98
    // **********************************************************************
99
100
    /**
101
     * Get the language name from language code.
102
     *
103
     * @param  string $languageCode
104
     * @return string
105
     */
106
    public function getSelectedLocaleName($languageCode)
107
    {
108
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
109
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
110
111
        return $ret;
112
    }
113
}
114