Completed
Push — develop ( bf2b91...c4d525 )
by Mohamed
06:31
created

User::getImageAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 Illuminate\Auth\Authenticatable;
15
use Illuminate\Auth\Passwords\CanResetPassword;
16
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
17
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
18
use Illuminate\Database\Eloquent;
19
use Illuminate\Database\Eloquent\Model;
20
use Thomaswelton\LaravelGravatar\Gravatar;
21
use Tinyissue\Model\Project\Issue;
22
use Auth as Auth;
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
 */
39
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
40
{
41
    use Authenticatable,
42
        CanResetPassword,
43
        Traits\User\CountTrait,
44
        Traits\User\RelationTrait,
45
        Traits\User\CrudTrait,
46
        Traits\User\QueryTrait;
47
48
    /**
49
     * User name is private.
50
     *
51
     * @var int
52
     */
53
    const PRIVATE_YES = 1;
54
55
    /**
56
     * User name is public.
57
     *
58
     * @var int
59
     */
60
    const PRIVATE_NO = 0;
61
62
    /**
63
     * User status Deleted.
64
     *
65
     * @var int
66
     */
67
    const DELETED_USERS = 1;
68
69
    /**
70
     * User status not deleted.
71
     *
72
     * @var int
73
     */
74
    const NOT_DELETED_USERS = 0;
75
76
    /**
77
     * The database table used by the model.
78
     *
79
     * @var string
80
     */
81
    protected $table = 'users';
82
83
    /**
84
     * The attributes that are mass assignable.
85
     *
86
     * @var array
87
     */
88
    protected $fillable = ['deleted', 'email', 'password', 'firstname', 'lastname', 'role_id', 'private', 'language'];
89
90
    /**
91
     * The attributes excluded from the model's JSON form.
92
     *
93
     * @var array
94
     */
95
    protected $hidden = ['password', 'remember_token'];
96
97
    /**
98
     * Collection of user permissions.
99
     *
100
     * @var Eloquent\Collection
101
     */
102
    protected $permission;
103
104
    /**
105
     * Get available languages from translations folder.
106
     *
107
     * @return array
108 3
     */
109
    public static function getLanguages()
110 3
    {
111
        $languages = [];
112 3
113 3
        $cdir = scandir(__DIR__ . '/../../resources/lang');
114 3
        foreach ($cdir as $value) {
115 3
            if (!in_array($value, ['.', '..'])) {
116
                $languages[$value] = $value;
117
            }
118
        }
119 3
120
        return $languages;
121
    }
122
123
    /**
124
     * Checks to see if $this user is current user.
125
     *
126
     * @return bool
127 3
     */
128
    public function me()
129 3
    {
130
        return $this->id == \Auth::user()->id;
131
    }
132
133
    /**
134
     * Whether or not the user has a valid permission in current context
135
     * e.g. can access the issue or the project.
136
     *
137
     * @param array $params
138
     *
139
     * @return bool
140 39
     */
141
    public function permissionInContext(array $params)
142
    {
143 39
        // Can access all projects
144 35
        if ($this->permission(Permission::PERM_PROJECT_ALL)) {
145
            return true;
146
        }
147 8
148
        $project = array_get($params, 'project', function () use ($params) {
149
            $issue = array_get($params, 'issue');
150
            if ($issue instanceof Issue) {
151
                return $issue->project;
152
            }
153
154 8
            return;
155
        });
156
157 8
        // Is member of the project
158 5
        if ($project && !$project->isMember($this->id)) {
159
            return false;
160
        }
161 7
162
        return true;
163
    }
164
165
    /**
166
     * Whether or not the user has a permission.
167
     *
168
     * @param string $key
169
     *
170
     * @return bool
171 54
     */
172
    public function permission($key)
173 54
    {
174 54
        $this->loadPermissions();
175 54
        foreach ($this->permission as $permission) {
176 54
            if ($permission->permission->isEqual($key)) {
177
                return true;
178
            }
179
        }
180 28
181
        return false;
182
    }
183
184
    /**
185
     * Return user full name with property "fullname".
186
     *
187
     * @return string
188 39
     */
189
    public function getFullNameAttribute()
190 39
    {
191 1
        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...
192
            return trans('tinyissue.anonymous');
193
        }
194 39
195
        return $this->attributes['firstname'] . ' ' . $this->attributes['lastname'];
196
    }
197
198
    /**
199
     * Return user image.
200
     *
201
     * @return string
202
     */
203
    public function getImageAttribute()
204
    {
205
        return app('gravatar')->src($this->email);
206
    }
207
}
208