OptimusEncodedRouteKey::getRouteKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Cog\Laravel\Optimus\Traits;
4
5
trait OptimusEncodedRouteKey
6
{
7
    /**
8
     * Get the value of the model's route key.
9
     *
10
     * @return mixed
11
     */
12
    public function getRouteKey()
13
    {
14
        $id = parent::getRouteKey();
15
16
        return $this->getOptimus()->encode($id);
0 ignored issues
show
Bug introduced by
The method encode() does not exist on Cog\Laravel\Optimus\OptimusManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

16
        return $this->getOptimus()->/** @scrutinizer ignore-call */ encode($id);
Loading history...
17
    }
18
19
    /**
20
     * Retrieve the model for a bound value.
21
     *
22
     * @param mixed $value
23
     * @param string|null $field
24
     * @return \Illuminate\Database\Eloquent\Model|null
25
     */
26
    public function resolveRouteBinding($value, $field = null)
27
    {
28
        if ($field === null) {
29
            $field = $this->getRouteKeyName();
0 ignored issues
show
Bug introduced by
The method getRouteKeyName() does not exist on Cog\Laravel\Optimus\Traits\OptimusEncodedRouteKey. 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

29
            /** @scrutinizer ignore-call */ 
30
            $field = $this->getRouteKeyName();

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...
30
        }
31
32
        if (is_string($value) && ctype_digit($value)) {
33
            $value = (int) $value;
34
        }
35
36
        if (is_int($value) && $field === $this->getRouteKeyName()) {
37
            $value = $this->getOptimus()->decode($value);
0 ignored issues
show
Bug introduced by
The method decode() does not exist on Cog\Laravel\Optimus\OptimusManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

37
            $value = $this->getOptimus()->/** @scrutinizer ignore-call */ decode($value);
Loading history...
38
        }
39
40
        return $this->where($field, $value)->first();
0 ignored issues
show
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

40
        return $this->/** @scrutinizer ignore-call */ where($field, $value)->first();
Loading history...
41
    }
42
43
    /**
44
     * Retrieve the model for a bound value.
45
     *
46
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation  $query
47
     * @param  mixed  $value
48
     * @param  string|null  $field
49
     * @return \Illuminate\Database\Eloquent\Relations\Relation
50
     */
51
    public function resolveRouteBindingQuery($query, $value, $field = null)
52
    {
53
        if ($field === null) {
54
            $field = $query->getRouteKeyName();
55
        }
56
57
        if (is_string($value) && ctype_digit($value)) {
58
            $value = (int) $value;
59
        }
60
61
        if (is_int($value) && $field === $this->getRouteKeyName()) {
62
            $value = $this->getOptimus()->decode($value);
63
        }
64
65
        return $query->where($field, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->where($field, $value) also could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the documented return type Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
66
    }
67
68
    /**
69
     * Get the Optimus instance.
70
     *
71
     * @return \Cog\Laravel\Optimus\OptimusManager
72
     */
73
    protected function getOptimus()
74
    {
75
        $connection = null;
76
77
        if (property_exists($this, 'optimusConnection')) {
78
            $connection = $this->optimusConnection;
79
        }
80
81
        return app('optimus')->connection($connection);
82
    }
83
}
84