Completed
Push — master ( 03784e...5cbfb6 )
by Arthur
03:31 queued 10s
created

ResolvesValidation::validationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Larapie\Actions\Concerns;
4
5
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
6
use Illuminate\Validation\ValidationException;
7
use Illuminate\Validation\Validator;
8
9
trait ResolvesValidation
10
{
11
    protected $errorBag = 'default';
12
13
    protected $validator;
14
15 1
    public function validate($rules = [], $messages = [], $customAttributes = [])
16
    {
17 1
        return app(ValidationFactory::class)
18 1
             ->make($this->validationData(), $rules, $messages, $customAttributes)
19 1
             ->validate();
20
    }
21
22 41
    public function passesValidation()
23
    {
24 41
        return $this->getValidatorInstance()->passes();
25
    }
26
27 41
    public function setValidator(Validator $validator)
28
    {
29 41
        $this->validator = $validator;
30
31 41
        return $this;
32
    }
33
34 1
    public function validated()
35
    {
36 1
        return $this->validator->validated();
37
    }
38
39 35
    public function rules()
40
    {
41 35
        return [];
42
    }
43
44 40
    public function messages()
45
    {
46 40
        return [];
47
    }
48
49 40
    public function attributes()
50
    {
51 40
        return [];
52
    }
53
54 39
    protected function resolveValidation()
55
    {
56 39
        if (! $this->passesValidation()) {
57 4
            $this->failedValidation();
58
        }
59
60 35
        return $this;
61
    }
62
63 41
    protected function getValidatorInstance()
64
    {
65 41
        if ($this->validator) {
66 3
            return $this->validator;
67
        }
68
69 41
        $factory = app(ValidationFactory::class);
70
71 41
        $validator = method_exists($this, 'validator')
72 1
            ? $this->validator($factory)
73 41
            : $this->createDefaultValidator($factory);
74
75 41
        if (method_exists($this, 'withValidator')) {
76 1
            $this->resolveAndCall($this, 'withValidator', compact('validator'));
0 ignored issues
show
Bug introduced by
It seems like resolveAndCall() 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

76
            $this->/** @scrutinizer ignore-call */ 
77
                   resolveAndCall($this, 'withValidator', compact('validator'));
Loading history...
77
        }
78
79 41
        if (method_exists($this, 'afterValidator')) {
80
            $validator->after(function ($validator) {
81 1
                $this->resolveAndCall($this, 'afterValidator', compact('validator'));
82 1
            });
83
        }
84
85 41
        $this->setValidator($validator);
86
87 41
        return $this->validator;
88
    }
89
90 40
    protected function createDefaultValidator(ValidationFactory $factory)
91
    {
92 40
        return $factory->make(
93 40
            $this->validationData(), $this->rules(),
94 40
            $this->messages(), $this->attributes()
95
        );
96
    }
97
98 4
    protected function failedValidation()
99
    {
100 4
        throw new \Larapie\Actions\Exception\ValidationException($this->validator);
101
    }
102
103
    protected function getRedirectUrl()
104
    {
105
        return redirect()->getUrlGenerator()->previous();
106
    }
107
108 40
    protected function validationData()
109
    {
110 40
        return $this->all();
0 ignored issues
show
Bug introduced by
It seems like all() 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

110
        return $this->/** @scrutinizer ignore-call */ all();
Loading history...
111
    }
112
}
113