Hashidable::getHashedIdAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace App\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Vinkla\Hashids\Facades\Hashids;
7
8
trait Hashidable
9
{
10
    /**
11
     * Retrieve model hashed_id attribute.
12
     *
13
     * @return string
14
     */
15
    public function getHashedIdAttribute()
16
    {
17
        return static::getEncodedId($this);
0 ignored issues
show
Bug introduced by
$this of type App\Traits\Hashidable is incompatible with the type Illuminate\Database\Eloquent\Model|integer expected by parameter $model of App\Traits\Hashidable::getEncodedId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
        return static::getEncodedId(/** @scrutinizer ignore-type */ $this);
Loading history...
18
    }
19
20
    /**
21
     * Retrieve the model for a bound value.
22
     *
23
     * @param  mixed                                      $value
24
     * @param  string|null                                $field
25
     * @return \Illuminate\Database\Eloquent\Model|null
26
     */
27
    public function resolveRouteBinding($value, $field = null)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

27
    public function resolveRouteBinding($value, /** @scrutinizer ignore-unused */ $field = null)

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

Loading history...
28
    {
29
        $value = static::getDecodedId($value);
30
31
        return $this->where($this->getRouteKeyName(), $value)->first();
0 ignored issues
show
Bug introduced by
The method getRouteKeyName() does not exist on App\Traits\Hashidable. Did you maybe mean getRouteKey()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return $this->where($this->/** @scrutinizer ignore-call */ getRouteKeyName(), $value)->first();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return $this->/** @scrutinizer ignore-call */ where($this->getRouteKeyName(), $value)->first();
Loading history...
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public static function getHashidConnection()
38
    {
39
        return get_called_class();
40
    }
41
42
    /**
43
     * @param  \Illuminate\Database\Eloquent\Model|int $model
44
     * @return mixed
45
     */
46
    public static function getEncodedId($model)
47
    {
48
        $id = $model instanceof Model ? $model->id : $model;
49
50
        return Hashids::connection(static::getHashidConnection())->encode($id);
51
    }
52
53
    /**
54
     * @param  string  $hashedId
55
     * @return mixed
56
     */
57
    public static function getDecodedId($hashedId)
58
    {
59
        return Hashids::connection(static::getHashidConnection())->decode($hashedId)[0] ?? null;
60
    }
61
62
    /**
63
     * Get the value of the model's route key.
64
     *
65
     * @return mixed
66
     */
67
    public function getRouteKey()
68
    {
69
        return static::getEncodedId($this);
0 ignored issues
show
Bug introduced by
$this of type App\Traits\Hashidable is incompatible with the type Illuminate\Database\Eloquent\Model|integer expected by parameter $model of App\Traits\Hashidable::getEncodedId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        return static::getEncodedId(/** @scrutinizer ignore-type */ $this);
Loading history...
70
    }
71
72
    /**
73
     * Scope a query to get with hased ID.
74
     *
75
     * @param  \Illuminate\Database\Eloquent\Builder   $query
76
     * @param  string                                  $hashedId
77
     * @return \Illuminate\Database\Eloquent\Builder
78
     */
79
    public function scopeFindByHashedId($query, string $hashedId)
80
    {
81
        $value = static::getDecodedId($hashedId);
82
83
        return $query->where($this->getRouteKeyName(), $value);
84
    }
85
86
    /**
87
     * Scope a query to get with hased ID.
88
     *
89
     * @param  string                              $hashedId
90
     * @param  array                               $columns
91
     * @return \Illuminate\Database\Eloquent\Model $model
92
     */
93
    public static function findByHashedIdOrFail(string $hashedId, $columns = ['*'])
94
    {
95
        $value = static::getDecodedId($hashedId);
96
97
        return static::where((new static )->getRouteKeyName(), $value)->select($columns)->firstOrFail();
98
    }
99
}
100