Passed
Pull Request — master (#648)
by John
06:12
created

User   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 3
Metric Value
eloc 70
c 4
b 1
f 3
dl 0
loc 160
rs 9.6
wmc 35

13 Methods

Rating   Name   Duplication   Size   Complexity  
A hasIndependentEmail() 0 2 2
A banneds() 0 2 1
A announcements() 0 2 1
A hasIndependentPassword() 0 2 1
A permissions() 0 2 1
A imagehostings() 0 2 1
A hasPermission() 0 2 1
A isIndependent() 0 2 2
A extras() 0 2 1
A getReadableNameAttribute() 0 3 1
B getExtra() 0 26 11
A getSocialiteInfo() 0 21 5
B setExtra() 0 28 7
1
<?php
2
3
namespace App\Models\Eloquent;
4
5
use Illuminate\Notifications\Notifiable;
6
use Illuminate\Contracts\Auth\MustVerifyEmail;
7
use Illuminate\Foundation\Auth\User as Authenticatable;
8
use Laravel\Passport\HasApiTokens;
9
use App\Models\Eloquent\UserExtra;
10
use PDO;
11
12
class User extends Authenticatable
13
{
14
    use HasApiTokens, Notifiable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $email which is not provided by App\Models\Eloquent\User.
Loading history...
15
16
    protected $table='users';
17
18
    /**
19
     * The attributes that are mass assignable.
20
     *
21
     * @var array
22
     */
23
    protected $fillable=[
24
        'name', 'email', 'password', 'avatar', 'contest_account'
25
    ];
26
27
    /**
28
     * The attributes that should be hidden for arrays.
29
     *
30
     * @var array
31
     */
32
    protected $hidden=[
33
        'password', 'remember_token', 'tokens'
34
    ];
35
36
    public function banneds() {
37
        return $this->hasMany('App\Models\Eloquent\UserBanned');
38
    }
39
40
    public function announcements() {
41
        return $this->hasMany('App\Models\Eloquent\Announcement');
42
    }
43
44
    public function permissions() {
45
        return $this->hasMany('App\Models\Eloquent\UserPermission');
46
    }
47
48
    public function imagehostings() {
49
        return $this->hasMany('App\Models\Eloquent\Tool\ImageHosting');
50
    }
51
52
    public function extras() {
53
        return $this->hasMany('App\Models\Eloquent\UserExtra', 'uid');
54
    }
55
56
    public function hasPermission($permissionID) {
57
        return ($this->permissions()->where(['permission_id'=>$permissionID])->count())>0;
58
    }
59
60
    public function hasIndependentPassword() {
61
        return filled($this->password);
62
    }
63
64
    public function hasIndependentEmail() {
65
        return !in_array(explode('@', $this->email)[1], ['temporary.email']) && !$this->contest_account;
66
    }
67
68
    public function isIndependent() {
69
        return $this->hasIndependentPassword() && $this->hasIndependentEmail();
70
    }
71
72
    public function getReadableNameAttribute()
73
    {
74
        return $this->name.' ('.$this->email.')';
75
    }
76
77
    /**
78
     * To get some extra info of a user.
79
     *
80
     * @param string|array $need An array is returned when an array is passed in, Only one value is returned when a string is passed in.
81
     * @param int|null $secretLevel the secret level this query currently running on
82
     * @return string|array $result
83
     */
84
    public function getExtra($need, $secretLevel=0) {
85
        $ret=$this->extras()->orderBy('key')->get()->toArray();
86
        $result=[];
87
        if (!empty($ret)) {
88
            if (is_string($need)) {
89
                foreach ($ret as $value) {
90
                    if (empty($value['secret_level']) || $value['secret_level']<=$secretLevel) {
91
                        $keyName=UserExtra::$extraMapping[$value['key']] ?? 'unknown';
92
                        if ($keyName==$need) {
93
                            return $value['value'];
94
                        }
95
                    }
96
                }
97
                return null;
98
            } else {
99
                foreach ($ret as $value) {
100
                    if (empty($value['secret_level']) || $value['secret_level']<=$secretLevel) {
101
                        $keyName=UserExtra::$extraMapping[$value['key']] ?? 'unknown';
102
                        if (in_array($keyName, $need)) {
103
                            $result[$keyName]=$value['value'];
104
                        }
105
                    }
106
                }
107
            }
108
        }
109
        return $result;
110
    }
111
112
    /**
113
     * To set some extra info of a user.
114
     *
115
     * @param string $keyName insert when key not found or update when key exists. Only values declared in UserExtra Model are accepted
116
     * @param string|null $value the extra info will be delete when value is null
117
     * @param int|null $secretLevel the secret level this query currently running on
118
     * @return mixed $result
119
     */
120
    public function setExtra($keyName, $value=null, $secretLevel=-1) {
121
        $key=array_search($keyName, UserExtra::$extraMapping);
122
        if ($key===false) {
123
            return false;
124
        }
125
        $ret=$this->extras()->where('key', $key)->limit(1)->get()->toArray();
126
        if (!empty($ret)) {
127
            $ret=$ret[0];
128
            unset($ret['id']);
129
            if (!is_null($value)) {
130
                $ret['value']=$value;
131
            } else {
132
                $this->extras()->where('key', $key)->delete();
133
                return true;
134
            }
135
            if ($secretLevel!=-1) {
136
                $ret['secret_level']=$secretLevel;
137
            }
138
            return $this->extras()->where('key', $key)->update($ret);
139
        } else {
140
            if ($value===null) {
141
                return true;
142
            }
143
            return $this->extras()->create([
144
                'key' => $key,
145
                'value' => $value,
146
                'secret_level' => $secretLevel==-1 ? 0 : $secretLevel,
147
            ])->id;
148
        }
149
    }
150
151
    public function getSocialiteInfo($secretLevel=-1)
0 ignored issues
show
Unused Code introduced by
The parameter $secretLevel is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

151
    public function getSocialiteInfo(/** @scrutinizer ignore-unused */ $secretLevel=-1)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
152
    {
153
        $socialites=[];
154
        foreach (UserExtra::$socialite_support as $key => $value) {
155
            $id_keyname=$key.'_id';
156
            $id=$this->getExtra($id_keyname);
157
            if (!empty($id)) {
158
                $info=[
159
                    'id' => $id,
160
                ];
161
                foreach ($value as $info_name) {
162
                    $info_temp=$this->getExtra($key.'_'.$info_name);
163
                    if ($info_temp!==null) {
164
                        $info[$info_name]=$info_temp;
165
                    }
166
                }
167
                $socialites[$key]=$info;
168
            }
169
        }
170
171
        return $socialites;
172
    }
173
}
174