Completed
Push — master ( f3c520...7ea85d )
by Phecho
03:40
created

User::isApproved()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
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
# == Schema Information
13
#
14
# Table name: users
15
#
16
#  id             :integer          not null, primary key
17
#  username       :string(255)
18
#  password       :string(255)
19
#  remember_token :string(100)
20
#  email          :integer
21
#  api_key        :string(255)
22
#  active         :boolean          default(FALSE)
23
#  level          :integer          default(2)
24
#  created_at     :timestamp
25
#  updated_at     :timestamp
26
#
27
28
namespace Gitamin\Models;
29
30
use AltThree\Validator\ValidatingTrait;
31
use Illuminate\Auth\Authenticatable;
32
use Illuminate\Auth\Passwords\CanResetPassword;
33
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
34
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
35
use Illuminate\Database\Eloquent\Model;
36
use Illuminate\Database\Eloquent\ModelNotFoundException;
37
use Illuminate\Support\Facades\Hash;
38
use Zizaco\Entrust\Traits\EntrustUserTrait;
39
40
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
41
{
42
    use Authenticatable, CanResetPassword, ValidatingTrait, EntrustUserTrait;
43
44
    /**
45
     * The attributes that should be casted to native types.
46
     *
47
     * @var string[]
48
     */
49
    protected $casts = [
50
        'id' => 'int',
51
        'username' => 'string',
52
        'email' => 'string',
53
        'api_key' => 'string',
54
        'active' => 'bool',
55
        'level' => 'int',
56
    ];
57
58
    /**
59
     * The properties that cannot be mass assigned.
60
     *
61
     * @var string[]
62
     */
63
    protected $guarded = [];
64
65
    /**
66
     * The hidden properties.
67
     *
68
     * These are excluded when we are serializing the model.
69
     *
70
     * @var string[]
71
     */
72
    protected $hidden = ['password', 'remember_token'];
73
74
    /**
75
     * The validation rules.
76
     *
77
     * @var string[]
78
     */
79
    public $rules = [
80
        'username' => ['required', 'regex:/\A(?!.*[:;]-\))[ -~]+\z/'],
81
        'email' => 'required|email',
82
        'password' => 'required',
83
    ];
84
85
    /**
86
     * Overrides the models boot method.
87
     */
88
    public static function boot()
89
    {
90
        parent::boot();
91
92
        self::creating(function ($user) {
93
            if (! $user->api_key) {
94
                $user->api_key = self::generateApiKey();
95
            }
96
        });
97
    }
98
99
    /**
100
     * Hash any password being inserted by default.
101
     *
102
     * @param string $password
103
     *
104
     * @return \Gitamin\Models\User
105
     */
106
    public function setPasswordAttribute($password)
107
    {
108
        $this->attributes['password'] = Hash::make($password);
109
110
        return $this;
111
    }
112
113
    /**
114
     * Returns a Gravatar URL for the users email address.
115
     *
116
     * @param int $size
117
     *
118
     * @return string
119
     */
120
    public function getGravatarAttribute($size = 200)
0 ignored issues
show
Unused Code introduced by
The parameter $size is not used and could be removed.

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

Loading history...
121
    {
122
        return 'https://avatars2.githubusercontent.com/u/15867969?v=3&s=40';
123
        //return sprintf('https://www.gravatar.com/avatar/%s?size=%d', md5($this->email), $size);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
    }
125
126
    /**
127
     * Find by api_key, or throw an exception.
128
     *
129
     * @param string   $token
130
     * @param string[] $columns
131
     *
132
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
133
     *
134
     * @return \Gitamin\Models\User
135
     */
136
    public static function findByApiToken($token, $columns = ['*'])
137
    {
138
        $user = static::where('api_key', $token)->first($columns);
139
140
        if (! $user) {
141
            throw new ModelNotFoundException();
142
        }
143
144
        return $user;
145
    }
146
147
    /**
148
     * Returns an API key.
149
     *
150
     * @return string
151
     */
152
    public static function generateApiKey()
153
    {
154
        return str_random(20);
155
    }
156
157
    /**
158
     * Returns whether a user is approved.
159
     *
160
     * @return bool
161
     */
162
    public function isApproved()
163
    {
164
        return $this->active == 1;
0 ignored issues
show
Documentation introduced by
The property active does not exist on object<Gitamin\Models\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...
165
    }
166
167
    /**
168
     * Returns whether a user is at admin level.
169
     *
170
     * @return bool
171
     */
172
    public function getIsAdminAttribute()
173
    {
174
        return $this->level === 1;
0 ignored issues
show
Documentation introduced by
The property level does not exist on object<Gitamin\Models\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...
175
    }
176
177
    /**
178
     * A user can have many issues.
179
     *
180
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
181
     */
182
    public function issues()
183
    {
184
        return $this->hasMany(Issue::class, 'user_id', 'id');
185
    }
186
}
187