HasherTrait::getHashedIdAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Auth\Models\Presenters;
2
3
use Illuminate\Database\Eloquent\Builder;
4
5
/**
6
 * Class     HasherTrait
7
 *
8
 * @package  Arcanesoft\Auth\Models\Presenters
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int     id
12
 * @property  string  hashed_id
13
 *
14
 * @method  static  \Illuminate\Database\Eloquent\Builder  withHashedId(string $hashedId)
15
 */
16
trait HasherTrait
17
{
18
    /* -----------------------------------------------------------------
19
     |  Scopes
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * Scope with the hashed id.
25
     *
26
     * @param  \Illuminate\Database\Eloquent\Builder  $query
27
     * @param  string                                 $hashedId
28
     *
29
     * @return \Illuminate\Database\Eloquent\Builder
30
     */
31
    public function scopeWithHashedId(Builder $query, $hashedId)
32
    {
33
        return $query->where('id', self::decodeHashedId($hashedId));
34
    }
35
36
    /* -----------------------------------------------------------------
37
     |  Getters & Setters
38
     | -----------------------------------------------------------------
39
     */
40
41
    /**
42
     * Get the model hash id.
43
     *
44
     * @return string
45
     */
46
    public function getHashedIdAttribute()
47
    {
48
        return self::hasher()->encode($this->id);
49
    }
50
51
    /* -----------------------------------------------------------------
52
     |  Other Methods
53
     | -----------------------------------------------------------------
54
     */
55
56
    /**
57
     * Get the hasher.
58
     *
59
     * @return \Arcanedev\Hasher\Contracts\HashManager
60
     */
61
    protected static function hasher()
62
    {
63
        return hasher();
64
    }
65
66
    /**
67
     * Decode the hashed id.
68
     *
69
     * @param  string  $hashedId
70
     *
71
     * @return int
72
     */
73
    protected static function decodeHashedId($hashedId)
74
    {
75
        return self::hasher()->decode($hashedId);
76
    }
77
}
78