Completed
Push — develop ( b16730...136b9c )
by Mohamed
07:35
created

User::getLanguages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3
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
use Illuminate\Routing\Route;
22
use Tinyissue\Model\Project\Issue;
23
24
/**
25
 * User is model class for users.
26
 *
27
 * @author Mohamed Alsharaf <[email protected]>
28
 *
29
 * @property int    $id
30
 * @property int    $deleted
31
 * @property int    $role_id
32
 * @property string $language
33
 * @property string $email
34
 * @property string $password
35
 * @property string $firstname
36
 * @property string $lastname
37
 * @property string $fullname
38
 * @property int    $status
39
 */
40
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
41
{
42
    use Authenticatable,
43
        CanResetPassword,
44
        Traits\User\CountTrait,
45
        Traits\User\RelationTrait,
46
        Traits\User\CrudTrait,
47
        Traits\User\QueryTrait;
48
49
    /**
50
     * User name is private.
51
     *
52
     * @var int
53
     */
54
    const PRIVATE_YES = 1;
55
56
    /**
57
     * User name is public.
58
     *
59
     * @var int
60
     */
61
    const PRIVATE_NO = 0;
62
63
    /**
64
     * User status Deleted.
65
     *
66
     * @var int
67
     */
68
    const DELETED_USERS = 1;
69
70
    /**
71
     * User status not deleted.
72
     *
73
     * @var int
74
     */
75
    const NOT_DELETED_USERS = 0;
76
77
    /**
78
     * User status active. (Standard).
79
     *
80
     * @var int
81
     */
82
    const ACTIVE_USER = 1;
83
84
    /**
85
     * User status blocked. (Too many login attempts).
86
     *
87
     * @var int
88
     */
89
    const BLOCKED_USER = 2;
90
91
    /**
92
     * User status inactive. (Cannot login at the moment).
93
     *
94
     * @var int
95
     */
96
    const INACTIVE_USER = 0;
97
98
    /**
99
     * The database table used by the model.
100
     *
101
     * @var string
102
     */
103
    protected $table = 'users';
104
105
    /**
106
     * The attributes that are mass assignable.
107
     *
108
     * @var array
109
     */
110
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language', 'status'];
111
112
    /**
113
     * The attributes excluded from the model's JSON form.
114
     *
115
     * @var array
116
     */
117
    protected $hidden = ['password', 'remember_token'];
118
119
    /**
120
     * Collection of user permissions.
121
     *
122
     * @var Eloquent\Collection
123
     */
124
    protected $permission;
125
126
    /**
127
     * Get available languages from translations folder.
128
     *
129
     * @return array
130
     */
131 3
    public static function getLanguages()
132
    {
133 3
        $languages = [];
134
135 3
        $cdir = scandir(__DIR__ . '/../../resources/lang');
136 3
        foreach ($cdir as $value) {
137 3
            if (!in_array($value, ['.', '..'])) {
138 3
                $languages[$value] = $value;
139
            }
140
        }
141
142 3
        return $languages;
143
    }
144
145
    /**
146
     * Checks to see if $this user is current user.
147
     *
148
     * @return bool
149
     */
150 3
    public function me()
151
    {
152 3
        return $this->id == \Auth::user()->id;
153
    }
154
155
    /**
156
     * Whether or not the user has a valid permission in current context
157
     * e.g. can access the issue or the project.
158
     *
159
     * @param Route $route
160
     *
161
     * @return bool
162
     */
163 39
    public function permissionInContext(Route $route)
164
    {
165
        // Can access all projects
166 39
        if ($this->permission(Permission::PERM_PROJECT_ALL)) {
167 35
            return true;
168
        }
169
170 8
        $project = $route->getParameter('project');
171 8
        $issue   = $route->getParameter('issue');
172 8
        if (!$project instanceof Project && $issue instanceof Issue) {
173
            $project = $issue->project;
174
        }
175
176
        // Is member of the project
177 8
        if ($project instanceof Project && !$project->isMember($this->id)) {
178 5
            return false;
179
        }
180
181 7
        return true;
182
    }
183
184
    /**
185
     * Whether or not the user has a permission.
186
     *
187
     * @param string $key
188
     *
189
     * @return bool
190
     */
191 60
    public function permission($key)
192
    {
193 60
        $this->loadPermissions();
194 60
        foreach ($this->permission as $permission) {
195 60
            if ($permission->permission->isEqual($key)) {
196 60
                return true;
197
            }
198
        }
199
200 27
        return false;
201
    }
202
203
    /**
204
     * Return user full name with property "fullname".
205
     *
206
     * @return string
207
     */
208 44
    public function getFullNameAttribute()
209
    {
210 44
        if ($this->private && (Auth::guest() || !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...
211 1
            return trans('tinyissue.anonymous');
212
        }
213
214 44
        return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
215
    }
216
217
    /**
218
     * Return user image.
219
     *
220
     * @return string
221
     */
222 5
    public function getImageAttribute()
223
    {
224 5
        return app('gravatar')->src($this->email);
225
    }
226
227
    /**
228
     * Returns list of user statuses.
229
     *
230
     * @return array
231
     */
232 2
    public static function getStatuses()
233
    {
234
        return [
235 2
            static::ACTIVE_USER     => trans('tinyissue.active'),
236 2
            static::BLOCKED_USER    => trans('tinyissue.blocked'),
237 2
            static::INACTIVE_USER   => trans('tinyissue.inactive'),
238
        ];
239
    }
240
241
    /**
242
     * Whether or not the user is active.
243
     *
244
     * @return bool
245
     */
246 7
    public function isActive()
247
    {
248 7
        return (int) $this->status === static::ACTIVE_USER;
249
    }
250
251
    /**
252
     * Whether or not the user is inactive.
253
     *
254
     * @return bool
255
     */
256
    public function isInactive()
257
    {
258
        return (int) $this->status === static::INACTIVE_USER;
259
    }
260
261
    /**
262
     * Whether or not the user is blocked.
263
     *
264
     * @return bool
265
     */
266
    public function isBlocked()
267
    {
268
        return (int) $this->status === static::BLOCKED_USER;
269
    }
270
271
    /**
272
     * Whether or not the user is normal user role.
273
     *
274
     * @return bool
275
     */
276 20
    public function isUser()
277
    {
278 20
        return $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...
279
    }
280
}
281