Passed
Push — master ( 83e6c6...5697af )
by Davide
56:57 queued 23:00
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\Foundation\Auth\Access\AuthorizesRequests;
7
use Illuminate\Foundation\Auth\User;
8
use Illuminate\Foundation\Bus\DispatchesJobs;
9
use Illuminate\Foundation\Validation\ValidatesRequests;
10
use Illuminate\Routing\Controller as BaseController;
11
use Illuminate\Support\Facades\Auth;
12
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
13
14
class Controller extends BaseController
15
{
16
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
17
18
    // https://stackoverflow.com/questions/51611015/authuser-return-null-5-6
19
    public function __construct()
20
    {
21
        $this->middleware(function ($request, $next) {
22
            $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...
23
24
            return $next($request);
25
        });
26
    }
27
28
    // **********************************************************************
29
30
    /**
31
     * Get the current logged user ID.
32
     * If user is admin or super admin return 0.
33
     *
34
     * @return \Illuminate\Foundation\Auth\User $ret
35
     */
36
    public function getLoggedUser()
37
    {
38
        $user = Auth::user();
39
40
        // This is needed to not get error in the queries with: ->when($loggedUser->id, function ($query, $loggedUserId) {
41
        /*if (! $user) {
42
            $user = new User();
43
            $user->name = null;
44
            $user->group = null;
45
        }*/
46
47
        $ret = $user;
48
49
        return $ret;
50
    }
51
52
    // **********************************************************************
53
54
    /**
55
     * Get the current logged user id.
56
     * (if admin or super admin returns 0).
57
     *
58
     * @return int|null $ret
59
     */
60 18
    public function getLoggedAuthorId()
61
    {
62 18
        $user = Auth::user();
63
64 18
        $ret = null;
65
66 18
        if ($user) {
67
            //@todo - disabled for the tests errors -- still to solve the isSuperAdmin()
68
            //$ret = (! $user->isSuperAdmin() && ! $user->isAdmin()) ? $user->id : 0;
69 15
            $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
72 18
        return $ret;
73
    }
74
75
    // **********************************************************************
76
77
    /**
78
     * Get the language name from language code.
79
     *
80
     * @param  string $languageCode
81
     * @return string
82
     */
83 4
    public function getSelectedLocaleName($languageCode)
84
    {
85 4
        $countriesAvailableForTranslations = LaravelLocalization::getSupportedLocales();
86 4
        $ret = $countriesAvailableForTranslations[$languageCode]['name'];
87
88 4
        return $ret;
89
    }
90
}
91