Completed
Push — master ( c73a7c...2189c9 )
by Albert
02:08
created

Resolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 6
c 5
b 1
f 0
lcom 1
cbo 3
dl 0
loc 98
ccs 23
cts 23
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolver() 0 6 1
A resolve() 0 10 1
A validator() 0 11 1
A createValidator() 0 8 2
1
<?php
2
3
namespace Proengsoft\JsValidation\Remote;
4
5
use Closure;
6
use Illuminate\Contracts\Validation\Factory;
7
use Illuminate\Validation\Validator as BaseValidator;
8
use Proengsoft\JsValidation\Support\AccessProtectedTrait;
9
10
class Resolver
11
{
12
    use AccessProtectedTrait;
13
14
    /**
15
     * @var Closure
16
     */
17
    protected $resolver;
18
19
    /**
20
     * @var Factory
21
     */
22
    protected $factory;
23
24
    /**
25
     * RemoteValidator constructor.
26
     *
27
     * @param Factory $factory
28
     */
29 6
    public function __construct(Factory $factory)
30
    {
31 6
        $this->factory = $factory;
32 6
        $this->resolver = $this->getProtected($factory, 'resolver');
33 6
    }
34
35
    /**
36
     * Closure used to resolve Validator instance.
37
     *
38
     * @param $field
39
     *
40
     * @return Closure
41
     */
42 5
    public function resolver($field)
43
    {
44
        return function ($translator, $data, $rules, $messages, $customAttributes) use ($field) {
45 3
            return $this->resolve($translator, $data, $rules, $messages, $customAttributes, $field);
46 5
        };
47
    }
48
49
    /**
50
     * Resolves Validator instance.
51
     *
52
     * @param $translator
53
     * @param $data
54
     * @param $rules
55
     * @param $messages
56
     * @param $customAttributes
57
     * @param $field
58
     *
59
     * @return Validator
60
     */
61 3
    protected function resolve($translator, $data, $rules, $messages, $customAttributes, $field)
62
    {
63 3
        $validator = $this->createValidator($translator, $data, $rules, $messages, $customAttributes);
64
65
        $validator->sometimes($field, Validator::EXTENSION_NAME, function () {
66 1
            return true;
67 3
        });
68
69 3
        return $validator;
70
    }
71
72
    /**
73
     * Create new validator instance.
74
     *
75
     * @param $translator
76
     * @param $data
77
     * @param $rules
78
     * @param $messages
79
     * @param $customAttributes
80
     * @return BaseValidator
81
     */
82 3
    protected function createValidator($translator, $data, $rules, $messages, $customAttributes)
83
    {
84 3
        if (is_null($this->resolver)) {
85 1
            return new BaseValidator($translator, $data, $rules, $messages, $customAttributes);
86
        }
87
88 2
        return call_user_func($this->resolver, $translator, $data, $rules, $messages, $customAttributes);
89
    }
90
91
    /**
92
     * Closure used to trigger JsValidations.
93
     *
94
     * @return Closure
95
     */
96
    public function validator()
97
    {
98 3
        return function ($attribute, $value, $parameters, BaseValidator $validator) {
99
100 1
            $data = $validator->getData();
101 1
            $validateAll = $data[$attribute.'_validate_all'];
102 1
            $remoteValidator = new Validator($validator);
103 1
            $remoteValidator->setValidateAll($validateAll);
104 1
            $remoteValidator->validate($attribute, $value, $parameters);
105 3
        };
106
    }
107
}
108