Completed
Push — master ( be6ba3...e40024 )
by ARCANEDEV
33:28
created

User::getGravatarAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Auth\Models;
2
3
use Arcanedev\LaravelAuth\Models\User as BaseUserModel;
4
5
/**
6
 * Class     User
7
 *
8
 * @package  Arcanesoft\Auth\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  string  gravatar
12
 */
13
class User extends BaseUserModel
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Getters & Setters
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Get the user hash id.
21
     *
22
     * @return string
23
     */
24
    public function getHashedIdAttribute()
25
    {
26
        return hasher()->encode($this->id);
27
    }
28
29
    /**
30
     * Get the full name attribute or use the username if empty.
31
     *
32
     * @return string
33
     */
34
    public function getFullNameAttribute()
35
    {
36
        $fullName = trim($this->first_name . ' ' . $this->last_name);
37
38
        return empty($fullName) ? $this->username : $fullName;
39
    }
40
41
    /**
42
     * Get the gravatar attribute.
43
     *
44
     * @return string
45
     */
46
    public function getGravatarAttribute()
47
    {
48
        return gravatar()
49
            ->setDefaultImage('mm')->setSize(160)
50
            ->src($this->email);
51
    }
52
53
    /**
54
     * Get the since date attribute (translated).
55
     *
56
     * @return string
57
     */
58
    public function getSinceDateAttribute()
59
    {
60
        return trans('auth::users.since', [
61
            'date' => $this->created_at->toFormattedDateString()
62
        ]);
63
    }
64
65
    /* ------------------------------------------------------------------------------------------------
66
     |  Main Function
67
     | ------------------------------------------------------------------------------------------------
68
     */
69
    /**
70
     * Get a user from a hashed id or fail if not found.
71
     *
72
     * @param  string  $hashedId
73
     *
74
     * @return self
75
     *
76
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
77
     */
78
    public static function firstHashedOrFail($hashedId)
79
    {
80
        $id = head(hasher()->decode($hashedId));
81
82
        return self::withTrashed()
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\SoftDeletes.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
83
            ->where('id', $id)
84
            ->firstOrFail();
85
    }
86
87
    /* ------------------------------------------------------------------------------------------------
88
     |  Check Functions
89
     | ------------------------------------------------------------------------------------------------
90
     */
91
    /**
92
     * Check if user is an administrator.
93
     *
94
     * @return bool
95
     */
96
    public function isAdmin()
97
    {
98
        return parent::isAdmin() || $this->is('administrator');
99
    }
100
}
101