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

RemoteValidationMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 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