Passed
Push — master ( c430e0...09e8fc )
by Davide
02:42 queued 11s
created

User::getUserGroupString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Notifications\Notifiable;
7
use Illuminate\Foundation\Auth\User as Authenticatable;
8
9
class User extends Authenticatable
10
{
11
    /***************************************************************************/
12
    /**
13
     * The table associated with the model.
14
     *
15
     * @var string
16
     */
17
    protected $table = 'users';
18
19
    /***************************************************************************/
20
21
    use Notifiable;
0 ignored issues
show
introduced by
The trait Illuminate\Notifications\Notifiable requires some properties which are not provided by DavideCasiraghi\LaravelEventsCalendar\Models\User: $email, $phone_number
Loading history...
22
23
    /**
24
     * The attributes that are mass assignable.
25
     *
26
     * @var array
27
     */
28
    protected $fillable = [
29
        'name', 'email', 'password', 'group', 'country_id', 'description', 'status', 'activation_code', 'accept_terms',
30
    ];
31
32
    /**
33
     * The attributes that should be hidden for arrays.
34
     *
35
     * @var array
36
     */
37
    protected $hidden = [
38
        'password', 'remember_token',
39
    ];
40
41
    /*
42
        To recognize admin - https://laracasts.com/discuss/channels/laravel/user-admin-authentication
43
    */
44
    protected $casts = [
45
        'group' => 'int',
46
    ];
47
48
    public function isSuperAdmin()
49
    {
50
        if ($this->group == 1) {
0 ignored issues
show
Bug introduced by
The property group does not seem to exist on DavideCasiraghi\LaravelEventsCalendar\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
51
            return true;
52
        } else {
53
            return false;
54
        }
55
    }
56
57
    public function isAdmin()
58
    {
59
        if ($this->group == 2) {
0 ignored issues
show
Bug introduced by
The property group does not seem to exist on DavideCasiraghi\LaravelEventsCalendar\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
60
            return true;
61
        } else {
62
            return false;
63
        }
64
    }
65
66
    /***************************************************************************/
67
68
    /**
69
     * Return the user group string.
70
     *
71
     * @param  \App\User  $post
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
     * @return string $ret - the user role description string
73
     */
74
    public static function getUserGroupString($group_id)
75
    {
76
        switch ($group_id) {
77
             case null:
78
                 $ret = 'Manager';
79
                 break;
80
81
             case 2:
82
                 $ret = 'Administrator';
83
                 break;
84
85
             case 1:
86
                 $ret = 'Super Administrator';
87
                 break;
88
         }
89
90
        return $ret;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
91
    }
92
93
    /***************************************************************************/
94
95
    /**
96
     * Return true if the user is logged as super admin.
97
     *
98
     * @param  none
99
     * @return string $ret - true if the user is super admin
100
     */
101
    public static function loggedAsSuperAdmin()
102
    {
103
        $user = Auth::user();
104
        if (! $user) {
105
            return false;
106
        } elseif ($user->group == 1) {
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...
107
            return true;
108
        } else {
109
            return false;
110
        }
111
    }
112
113
    /***************************************************************************/
114
115
    /**
116
     * Return true if the user is logged as admin.
117
     *
118
     * @param  none
119
     * @return string $ret - true if the user is admin
120
     */
121
    public static function loggedAsAdmin()
122
    {
123
        $user = Auth::user();
124
        if (! $user) {
125
            return false;
126
        } elseif ($user->group == 2) {
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...
127
            return true;
128
        } else {
129
            return false;
130
        }
131
    }
132
}
133