Completed
Push — master ( d778e6...6d1c30 )
by ARCANEDEV
08:16
created

HasherTrait::hasher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * Scope with the hashed id.
24
     *
25
     * @param  \Illuminate\Database\Eloquent\Builder  $query
26
     * @param  string                                 $hashedId
27
     *
28
     * @return \Illuminate\Database\Eloquent\Builder
29
     */
30
    public function scopeWithHashedId(Builder $query, $hashedId)
31
    {
32
        return $query->where('id', self::decodeHashedId($hashedId));
33
    }
34
35
    /* ------------------------------------------------------------------------------------------------
36
     |  Getters & Setters
37
     | ------------------------------------------------------------------------------------------------
38
     */
39
    /**
40
     * Get the model hash id.
41
     *
42
     * @return string
43
     */
44
    public function getHashedIdAttribute()
45
    {
46
        return self::hasher()->encode($this->id);
47
    }
48
49
    /* ------------------------------------------------------------------------------------------------
50
     |  Other Functions
51
     | ------------------------------------------------------------------------------------------------
52
     */
53
    /**
54
     * Get the hasher.
55
     *
56
     * @return \Arcanedev\Hasher\Contracts\HashManager
57
     */
58
    protected static function hasher()
59
    {
60
        return hasher();
61
    }
62
63
    /**
64
     * Decode the hashed id.
65
     *
66
     * @param  string  $hashedId
67
     *
68
     * @return int
69
     */
70
    protected static function decodeHashedId($hashedId)
71
    {
72
        return self::hasher()->decode($hashedId);
73
    }
74
}
75