Total Complexity | 47 |
Total Lines | 237 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class User extends Authenticatable |
||
17 | { |
||
18 | use HasApiTokens, Notifiable; |
||
|
|||
19 | |||
20 | protected $table='users'; |
||
21 | |||
22 | /** |
||
23 | * The attributes that are mass assignable. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $fillable=[ |
||
28 | 'name', 'email', 'password', 'avatar', 'contest_account' |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * The attributes that should be hidden for arrays. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $hidden=[ |
||
37 | 'password', 'remember_token', 'tokens' |
||
38 | ]; |
||
39 | |||
40 | public function submissions() |
||
41 | { |
||
42 | return $this->hasMany('App\Models\Eloquent\Submission', 'uid'); |
||
43 | } |
||
44 | |||
45 | public function banneds() { |
||
46 | return $this->hasMany('App\Models\Eloquent\UserBanned'); |
||
47 | } |
||
48 | |||
49 | public function announcements() { |
||
50 | return $this->hasMany('App\Models\Eloquent\Announcement'); |
||
51 | } |
||
52 | |||
53 | public function permissions() { |
||
54 | return $this->hasMany('App\Models\Eloquent\UserPermission'); |
||
55 | } |
||
56 | |||
57 | public function imagehostings() { |
||
58 | return $this->hasMany('App\Models\Eloquent\Tool\ImageHosting'); |
||
59 | } |
||
60 | |||
61 | public function extras() { |
||
62 | return $this->hasMany('App\Models\Eloquent\UserExtra', 'uid'); |
||
63 | } |
||
64 | |||
65 | public function hasPermission($permissionID) { |
||
66 | return ($this->permissions()->where(['permission_id'=>$permissionID])->count())>0; |
||
67 | } |
||
68 | |||
69 | public function hasIndependentPassword() { |
||
70 | return filled($this->password); |
||
71 | } |
||
72 | |||
73 | public function hasIndependentEmail() { |
||
75 | } |
||
76 | |||
77 | public function isIndependent() { |
||
78 | return $this->hasIndependentPassword() && $this->hasIndependentEmail(); |
||
79 | } |
||
80 | |||
81 | public function getReadableNameAttribute() |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * To get some extra info of a user. |
||
88 | * |
||
89 | * @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. |
||
90 | * @param int|null $secretLevel the secret level this query currently running on |
||
91 | * @return string|array $result |
||
92 | */ |
||
93 | public function getExtra($need, $secretLevel=0) { |
||
94 | $ret=$this->extras()->orderBy('key')->get()->toArray(); |
||
95 | $result=[]; |
||
96 | if (!empty($ret)) { |
||
97 | if (is_string($need)) { |
||
98 | foreach ($ret as $value) { |
||
99 | if (empty($value['secret_level']) || $value['secret_level']<=$secretLevel) { |
||
100 | $keyName=UserExtra::$extraMapping[$value['key']] ?? 'unknown'; |
||
101 | if ($keyName==$need) { |
||
102 | return $value['value']; |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | return null; |
||
107 | } else { |
||
108 | foreach ($ret as $value) { |
||
109 | if (empty($value['secret_level']) || $value['secret_level']<=$secretLevel) { |
||
110 | $keyName=UserExtra::$extraMapping[$value['key']] ?? 'unknown'; |
||
111 | if (in_array($keyName, $need)) { |
||
112 | $result[$keyName]=$value['value']; |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | return $result; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * To set some extra info of a user. |
||
123 | * |
||
124 | * @param string $keyName insert when key not found or update when key exists. Only values declared in UserExtra Model are accepted |
||
125 | * @param string|null $value the extra info will be delete when value is null |
||
126 | * @param int|null $secretLevel the secret level this query currently running on |
||
127 | * @return mixed $result |
||
128 | */ |
||
129 | public function setExtra($keyName, $value=null, $secretLevel=-1) { |
||
130 | $key=array_search($keyName, UserExtra::$extraMapping); |
||
131 | if ($key===false) { |
||
132 | return false; |
||
133 | } |
||
134 | $ret=$this->extras()->where('key', $key)->limit(1)->get()->toArray(); |
||
135 | if (!empty($ret)) { |
||
136 | $ret=$ret[0]; |
||
137 | unset($ret['id']); |
||
138 | if (!is_null($value)) { |
||
139 | $ret['value']=$value; |
||
140 | } else { |
||
141 | $this->extras()->where('key', $key)->delete(); |
||
142 | return true; |
||
143 | } |
||
144 | if ($secretLevel!=-1) { |
||
145 | $ret['secret_level']=$secretLevel; |
||
146 | } |
||
147 | return $this->extras()->where('key', $key)->update($ret); |
||
148 | } else { |
||
149 | if ($value===null) { |
||
150 | return true; |
||
151 | } |
||
152 | return $this->extras()->create([ |
||
153 | 'key' => $key, |
||
154 | 'value' => $value, |
||
155 | 'secret_level' => $secretLevel==-1 ? 0 : $secretLevel, |
||
156 | ])->id; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | public function getSocialiteInfo($secretLevel=-1) |
||
161 | { |
||
162 | $socialites=[]; |
||
163 | foreach (UserExtra::$socialite_support as $key => $value) { |
||
164 | $id_keyname=$key.'_id'; |
||
165 | $id=$this->getExtra($id_keyname); |
||
166 | if (!empty($id)) { |
||
167 | $info=[ |
||
168 | 'id' => $id, |
||
169 | ]; |
||
170 | foreach ($value as $info_name) { |
||
171 | $info_temp=$this->getExtra($key.'_'.$info_name); |
||
172 | if ($info_temp!==null) { |
||
173 | $info[$info_name]=$info_temp; |
||
174 | } |
||
175 | } |
||
176 | $socialites[$key]=$info; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $socialites; |
||
181 | } |
||
182 | |||
183 | public function problems_latest_submission($problems, $contestID = null, Carbon $till = null, $verdictFilter = []) |
||
208 | } |
||
209 | |||
210 | public function getDojoStatistics() |
||
211 | { |
||
212 | try { |
||
213 | $statistics = []; |
||
214 | $problemIDArr = DojoProblem::select('problem_id')->distinct()->get()->pluck('problem_id'); |
||
215 | |||
253 | } |
||
254 | } |
||
255 | } |
||
256 |