Completed
Push — 2.0 ( 689dde...a29dba )
by Nicolas
13:22
created

User::getFirstApiKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php namespace Modules\User\Entities\Sentinel;
2
3
use Cartalyst\Sentinel\Laravel\Facades\Activation;
4
use Cartalyst\Sentinel\Users\EloquentUser;
5
use Laracasts\Presenter\PresentableTrait;
6
use Modules\User\Entities\UserInterface;
7
use Modules\User\Entities\UserToken;
8
use Modules\User\Presenters\UserPresenter;
9
10
class User extends EloquentUser implements UserInterface
11
{
12
    use PresentableTrait;
13
14
    protected $fillable = [
15
        'email',
16
        'password',
17
        'permissions',
18
        'first_name',
19
        'last_name',
20
    ];
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    protected $loginNames = ['email'];
26
27
    protected $presenter = UserPresenter::class;
28
29
    public function __construct(array $attributes = [])
30
    {
31
        $this->loginNames = config('asgard.user.users.login-columns');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('asgard.user.users.login-columns') of type * is incompatible with the declared type array of property $loginNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
        $this->fillable = config('asgard.user.users.fillable');
0 ignored issues
show
Documentation Bug introduced by
It seems like config('asgard.user.users.fillable') of type * is incompatible with the declared type array of property $fillable.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
34
        parent::__construct($attributes);
35
    }
36
37
    /**
38
     * Checks if a user belongs to the given Role ID
39
     * @param  int $roleId
40
     * @return bool
41
     */
42
    public function hasRoleId($roleId)
43
    {
44
        return $this->roles()->whereId($roleId)->count() >= 1;
45
    }
46
47
    /**
48
     * Checks if a user belongs to the given Role Name
49
     * @param  string $name
50
     * @return bool
51
     */
52
    public function hasRoleName($name)
53
    {
54
        return $this->roles()->whereName($name)->count() >= 1;
55
    }
56
57
    /**
58
     * Check if the current user is activated
59
     * @return bool
60
     */
61
    public function isActivated()
62
    {
63
        if (Activation::completed($this)) {
64
            return true;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
72
     */
73
    public function api_keys()
74
    {
75
        return $this->hasMany(UserToken::class);
76
    }
77
78
    /**
79
     * Get the first available api key
80
     * @return string
81
     */
82
    public function getFirstApiKey()
83
    {
84
        $userToken = $this->api_keys->first();
85
86
        if ($userToken === null) {
87
            return '';
88
        }
89
90
        return $userToken->access_token;
91
    }
92
93
    public function __call($method, $parameters)
94
    {
95
        $class_name = class_basename($this);
96
97
        #i: Convert array to dot notation
98
        $config = implode('.', ['relations', $class_name, $method]);
99
100
        #i: Relation method resolver
101
        if (config()->has($config)) {
102
            $function = config()->get($config);
103
104
            return $function($this);
105
        }
106
107
        #i: No relation found, return the call to parent (Eloquent) to handle it.
108
        return parent::__call($method, $parameters);
109
    }
110
}
111