Passed
Push — master ( c66168...9f3414 )
by Ivan
01:54
created

RouteKeyExists   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 150
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A model() 0 4 1
A __construct() 0 4 1
A replace() 0 6 1
A add() 0 6 1
A passes() 0 12 2
A message() 0 6 1
A updateRequest() 0 12 3
A mergeRequest() 0 6 1
1
<?php
2
3
namespace CodeZero\RouteKeyExists;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class RouteKeyExists implements Rule
8
{
9
    /**
10
     * The model to validate.
11
     *
12
     * @var \Illuminate\Database\Eloquent\Model
13
     */
14
    protected $model;
15
16
    /**
17
     * The attribute being validated.
18
     *
19
     * @var string
20
     */
21
    protected $attribute;
22
23
    /**
24
     * Replace the route key in the request
25
     * with the actual database value?
26
     *
27
     * @var bool
28
     */
29
    protected $replaceAttribute = false;
30
31
    /**
32
     * Attribute with the actual database value
33
     * to add to the request.
34
     *
35
     * @var string
36
     */
37
    protected $addAttribute = null;
38
39
    /**
40
     * Create a new rule instance.
41
     *
42
     * @param string $model
43
     *
44
     * @return static
45
     */
46 3
    public static function model($model)
47
    {
48 3
        return new static($model);
49
    }
50
51
    /**
52
     * Create a new rule instance.
53
     *
54
     * @param string $model
55
     */
56 3
    public function __construct($model)
57
    {
58 3
        $this->model = new $model;
59 3
    }
60
61
    /**
62
     * Replace the route key in the request
63
     * with the actual database value.
64
     *
65
     * @return $this
66
     */
67 1
    public function replace()
68
    {
69 1
        $this->replaceAttribute = true;
70
71 1
        return $this;
72
    }
73
74
    /**
75
     * Add the given attribute with the actual
76
     * database value to the request.
77
     *
78
     * @param string $attribute
79
     *
80
     * @return $this
81
     */
82 1
    public function add($attribute)
83
    {
84 1
        $this->addAttribute = $attribute;
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * Determine if the validation rule passes.
91
     *
92
     * @param string $attribute
93
     * @param string $value
94
     *
95
     * @return bool
96
     */
97 3
    public function passes($attribute, $value)
98
    {
99 3
        $this->attribute = $attribute;
100
101 3
        if ( ! $model = $this->model->resolveRouteBinding($value)) {
102 1
            return false;
103
        }
104
105 3
        $this->updateRequest($model);
106
107 3
        return true;
108
    }
109
110
    /**
111
     * Get the validation error message.
112
     *
113
     * @return string
114
     */
115 1
    public function message()
116
    {
117 1
        return trans('validation.exists', [
118 1
            'attribute' => $this->attribute,
119
        ]);
120
    }
121
122
    /**
123
     * Update the request attributes if needed.
124
     *
125
     * @param \Illuminate\Database\Eloquent\Model $model
126
     *
127
     * @return void
128
     */
129 3
    protected function updateRequest($model)
130
    {
131 3
        $actualKey = $model->{$model->getRouteKeyName()};
132
133 3
        if ($this->replaceAttribute === true) {
134 1
            $this->mergeRequest($this->attribute, $actualKey);
135
        }
136
137 3
        if ($this->addAttribute !== null) {
138 1
            $this->mergeRequest($this->addAttribute, $actualKey);
139
        }
140 3
    }
141
142
    /**
143
     * Merge the request attributes with the given key / value pair.
144
     *
145
     * @param string $key
146
     * @param mixed $value
147
     *
148
     * @return void
149
     */
150 2
    protected function mergeRequest($key, $value)
151
    {
152 2
        request()->merge([
153 2
            $key => $value,
154
        ]);
155 2
    }
156
}
157