Completed
Push — develop ( c3857f...9623b5 )
by Mohamed
08:41
created

User::me()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model;
13
14
use Auth as Auth;
15
use Illuminate\Auth\Authenticatable;
16
use Illuminate\Auth\Passwords\CanResetPassword;
17
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
18
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
19
use Illuminate\Database\Eloquent;
20
use Illuminate\Database\Eloquent\Model;
21
22
/**
23
 * User is model class for users.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 *
27
 * @property int    $id
28
 * @property int    $deleted
29
 * @property int    $role_id
30
 * @property string $language
31
 * @property string $email
32
 * @property string $password
33
 * @property string $firstname
34
 * @property string $lastname
35
 * @property string $fullname
36
 * @property int    $status
37
 */
38
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
39
{
40
    use Authenticatable,
41
        CanResetPassword,
42
        Traits\User\CountTrait,
43
        Traits\User\RelationTrait,
44
        Traits\User\CrudTrait,
45
        Traits\User\QueryTrait;
46
47
    /**
48
     * User name is private.
49
     *
50
     * @var int
51
     */
52
    const PRIVATE_YES = 1;
53
54
    /**
55
     * User name is public.
56
     *
57
     * @var int
58
     */
59
    const PRIVATE_NO = 0;
60
61
    /**
62
     * User status Deleted.
63
     *
64
     * @var int
65
     */
66
    const DELETED_USERS = 1;
67
68
    /**
69
     * User status not deleted.
70
     *
71
     * @var int
72
     */
73
    const NOT_DELETED_USERS = 0;
74
75
    /**
76
     * User status active. (Standard).
77
     *
78
     * @var int
79
     */
80
    const ACTIVE_USER = 1;
81
82
    /**
83
     * User status blocked. (Too many login attempts).
84
     *
85
     * @var int
86
     */
87
    const BLOCKED_USER = 2;
88
89
    /**
90
     * User status inactive. (Cannot login at the moment).
91
     *
92
     * @var int
93
     */
94
    const INACTIVE_USER = 0;
95
96
    /**
97
     * The database table used by the model.
98
     *
99
     * @var string
100
     */
101
    protected $table = 'users';
102
103
    /**
104
     * The attributes that are mass assignable.
105
     *
106
     * @var array
107
     */
108
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status'];
109
110
    /**
111
     * The attributes excluded from the model's JSON form.
112
     *
113
     * @var array
114
     */
115
    protected $hidden = ['password', 'remember_token'];
116
117
    /**
118
     * Collection of user permissions.
119
     *
120
     * @var Eloquent\Collection
121
     */
122
    protected $permission;
123
124
    /**
125
     * Get available languages from translations folder.
126
     *
127
     * @return array
128
     */
129 5
    public static function getLanguages()
130
    {
131 5
        $languages = [];
132
133 5
        $cdir = scandir(__DIR__ . '/../../resources/lang');
134 5
        foreach ($cdir as $value) {
135 5
            if (!in_array($value, ['.', '..'])) {
136 5
                $languages[$value] = $value;
137
            }
138
        }
139
140 5
        return $languages;
141
    }
142
143
    /**
144
     * Checks to see if $this user is current user.
145
     *
146
     * @return bool
147
     */
148 3
    public function me()
149
    {
150 3
        return $this->id == \Auth::user()->id;
151
    }
152
153
    /**
154
     * Whether or not the user has a permission.
155
     *
156
     * @param string $key
157
     *
158
     * @return bool
159
     */
160 64
    public function permission($key)
161
    {
162 64
        $this->loadPermissions();
163 64
        foreach ($this->permission as $permission) {
164 64
            if ($permission->permission->isEqual($key)) {
165 64
                return true;
166
            }
167
        }
168
169 31
        return false;
170
    }
171
172
    /**
173
     * Return user full name with property "fullname".
174
     *
175
     * @return string
176
     */
177 47
    public function getFullNameAttribute()
178
    {
179 47
        if ($this->private && (Auth::guest() || (Auth::user()->id != $this->id && !Auth::user()->permission('administration')))) {
0 ignored issues
show
Documentation introduced by
The property private does not exist on object<Tinyissue\Model\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180 1
            return trans('tinyissue.anonymous');
181
        }
182
183 47
        return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
184
    }
185
186
    /**
187
     * Return user image.
188
     *
189
     * @return string
190
     */
191 6
    public function getImageAttribute()
192
    {
193 6
        return app('gravatar')->src($this->email);
194
    }
195
196
    /**
197
     * Returns list of user statuses.
198
     *
199
     * @return array
200
     */
201 2
    public static function getStatuses()
202
    {
203
        return [
204 2
            static::ACTIVE_USER     => trans('tinyissue.active'),
205 2
            static::BLOCKED_USER    => trans('tinyissue.blocked'),
206 2
            static::INACTIVE_USER   => trans('tinyissue.inactive'),
207
        ];
208
    }
209
210
    /**
211
     * Whether or not the user is active.
212
     *
213
     * @return bool
214
     */
215 7
    public function isActive()
216
    {
217 7
        return (int) $this->status === static::ACTIVE_USER;
218
    }
219
220
    /**
221
     * Whether or not the user is inactive.
222
     *
223
     * @return bool
224
     */
225
    public function isInactive()
226
    {
227
        return (int) $this->status === static::INACTIVE_USER;
228
    }
229
230
    /**
231
     * Whether or not the user is blocked.
232
     *
233
     * @return bool
234
     */
235
    public function isBlocked()
236
    {
237
        return (int) $this->status === static::BLOCKED_USER;
238
    }
239
240
    /**
241
     * Whether or not the user is normal user role.
242
     *
243
     * @return bool
244
     */
245 36
    public function isUser()
246
    {
247 36
        return $this->exists && $this->role->role === Role::ROLE_USER;
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<Tinyissue\Model\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
248
    }
249
250
    /**
251
     * Whether or not the user is administrator.
252
     *
253
     * @return bool
254
     */
255
    public function isAdmin()
256
    {
257
        return $this->exists && $this->role->role === Role::ROLE_ADMIN;
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<Tinyissue\Model\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
258
    }
259
}
260