Passed
Push — master ( 685e2a...9f4f72 )
by Iman
03:13
created

Validator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 33
c 4
b 1
f 0
dl 0
loc 89
ccs 41
cts 42
cp 0.9762
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapForIgnore() 0 3 1
A otherwise() 0 12 2
A validationPassesCallback() 0 7 1
A validatorCallback() 0 7 1
A __construct() 0 3 1
A makeValidator() 0 9 2
A __destruct() 0 18 4
A beforeValidationModifyData() 0 3 1
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Imanghafoori\HeyMan\Core\Reaction;
6
use Illuminate\Contracts\Validation\Factory;
7
use Imanghafoori\HeyMan\Switching\HeyManSwitcher;
8
9
final class Validator
10
{
11
    private $validationData;
12
13
    private $modifier;
14
15 9
    public function __construct(array $validationData)
16
    {
17 9
        $this->validationData = $validationData;
18 9
    }
19
20 1
    public function otherwise()
21
    {
22 1
        $rules = $this->validationData;
23 1
        $modifier = $this->modifier ?: function ($args) {
24 1
            return $args;
25 1
        };
26
27 1
        $result = $this->validationPassesCallback($modifier, $rules);
28
29 1
        resolve('heyman.chain')->set('condition', $result);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        resolve('heyman.chain')->/** @scrutinizer ignore-call */ set('condition', $result);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31 1
        return resolve(Reaction::class);
32
    }
33
34 1
    public function beforeValidationModifyData($callable)
35
    {
36 1
        $this->modifier = $callable;
37 1
    }
38
39
    public function validatorCallback($modifier, $rules)
40
    {
41 8
        $validator = function () use ($modifier, $rules) {
42 5
            $this->makeValidator($modifier, $rules)->validate();
43 8
        };
44
45 8
        return $this->wrapForIgnore($validator);
46
    }
47
48
    public function validationPassesCallback($modifier, $rules)
49
    {
50 1
        $validator = function () use ($modifier, $rules) {
51 1
            return ! $this->makeValidator($modifier, $rules)->fails();
52 1
        };
53
54 1
        return $this->wrapForIgnore($validator);
55
    }
56
57 6
    public function makeValidator($modifier, $rules)
58
    {
59 6
        if (is_callable($rules[0])) {
60 4
            $rules[0] = call_user_func($rules[0]);
61
        }
62
63 6
        $newData = app()->call($modifier, [request()->all()]);
64
65 6
        return resolve(Factory::class)->make($newData, ...$rules);
0 ignored issues
show
Bug introduced by
$rules is expanded, but the parameter $rules of Illuminate\Validation\Factory::make() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        return resolve(Factory::class)->make($newData, /** @scrutinizer ignore-type */ ...$rules);
Loading history...
Bug introduced by
It seems like $newData can also be of type callable; however, parameter $data of Illuminate\Validation\Factory::make() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        return resolve(Factory::class)->make(/** @scrutinizer ignore-type */ $newData, ...$rules);
Loading history...
66
    }
67
68 8
    public function __destruct()
69
    {
70
        try {
71 8
            $chain = app('heyman.chain');
72 8
            $condition = $chain->get('condition');
73
74 8
            if (! $condition) {
75 8
                $data = $this->validationData;
76 8
                $modifier = $this->modifier ?: function ($args) {
77 4
                    return $args;
78 8
                };
79
80 8
                $condition = $this->validatorCallback($modifier, $data);
81 8
                $chain->set('condition', $condition);
82
            }
83
84 8
            resolve('heyman.chains')->commitChain();
0 ignored issues
show
Bug introduced by
The method commitChain() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
            resolve('heyman.chains')->/** @scrutinizer ignore-call */ commitChain();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
        } catch (\Throwable $throwable) {
86
            //
87
        }
88 8
    }
89
90
    /**
91
     * @param \Closure $validator
92
     *
93
     * @return mixed
94
     */
95 9
    private function wrapForIgnore(\Closure $validator)
96
    {
97 9
        return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
98
}
99
}
100