Completed
Push — master ( 71ec50...18d3f1 )
by Albert
02:10
created

RemoteValidationMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 4
c 5
b 0
f 0
lcom 1
cbo 4
dl 0
loc 55
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 8 2
A wrapValidator() 0 6 1
1
<?php
2
3
namespace Proengsoft\JsValidation;
4
5
use Closure;
6
use Illuminate\Contracts\Config\Repository as Config;
7
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
8
use Proengsoft\JsValidation\Remote\Resolver;
9
use Proengsoft\JsValidation\Remote\Validator;
10
11
class RemoteValidationMiddleware
12
{
13
    /**
14
     * Validator factory instance to wrap.
15
     *
16
     * @var ValidationFactory
17
     */
18
    protected $factory;
19
20
    /**
21
     * Field used to detect Javascript validation.
22
     *
23
     * @var mixed
24
     */
25
    protected $field;
26
27
    /**
28
     * RemoteValidationMiddleware constructor.
29
     *
30
     * @param ValidationFactory $validator
31
     * @param Config            $config
32
     */
33 2
    public function __construct(ValidationFactory $validator, Config $config)
34
    {
35 2
        $this->factory = $validator;
36 2
        $this->field = $config->get('jsvalidation.remote_validation_field');
37 2
    }
38
39
    /**
40
     * Handle an incoming request.
41
     *
42
     * @param \Illuminate\Http\Request $request
43
     * @param Closure                  $next
44
     *
45
     * @return mixed
46
     */
47 2
    public function handle($request, Closure $next)
48
    {
49 2
        if ($request->has($this->field)) {
50 1
            $this->wrapValidator();
51 1
        }
52
53 2
        return $next($request);
54
    }
55
56
    /**
57
     * Wraps Validaroe resolver with RemoteValidator resolver.
58
     */
59 1
    protected function wrapValidator()
60
    {
61 1
        $resolver = new Resolver($this->factory);
62 1
        $this->factory->resolver($resolver->resolver($this->field));
63 1
        $this->factory->extend(Validator::EXTENSION_NAME, $resolver->validator());
64 1
    }
65
}
66