Completed
Push — master ( 12114f...959a47 )
by Iman
06:16 queued 01:24
created

Validator::wrapForIgnore()   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
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 12
    public function __construct(array $validationData)
16
    {
17 12
        $this->validationData = $validationData;
18 12
    }
19
20 3
    public function otherwise()
21
    {
22 3
        $rules = $this->validationData;
23 3
        $modifier = $this->modifier ?: function ($args) {
24 1
            return $args;
25 3
        };
26
27 3
        $result = $this->validationPassesCallback($modifier, $rules);
28
29 3
        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 3
        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 9
        $validator = function () use ($modifier, $rules) {
42 5
            $this->makeValidator($modifier, $rules)->validate();
43 9
        };
44
45 9
        return $this->wrapForIgnore($validator);
46
    }
47
48
    public function validationPassesCallback($modifier, $rules)
49
    {
50 3
        $validator = function () use ($modifier, $rules) {
51 1
            $v = $this->makeValidator($modifier, $rules);
52
53 1
            resolve('heyman.chain')->set('condition_meta', $v);
54
55 1
            return ! $v->fails();
56 3
        };
57
58 3
        return $this->wrapForIgnore($validator);
59
    }
60
61 6
    public function makeValidator($modifier, $rules)
62
    {
63 6
        if (is_callable($rules[0])) {
64 4
            $rules[0] = call_user_func($rules[0]);
65
        }
66
67 6
        $newData = app()->call($modifier, [request()->all()]);
68
69 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

69
        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

69
        return resolve(Factory::class)->make(/** @scrutinizer ignore-type */ $newData, ...$rules);
Loading history...
70
    }
71
72 10
    public function __destruct()
73
    {
74
        try {
75 10
            $chain = app('heyman.chain');
76 10
            $condition = $chain->get('condition');
77
78 10
            if ($condition) {
79 2
                return 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

79
                return 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...
80
            }
81
82 9
            $data = $this->validationData;
83 9
            $modifier = $this->modifier ?: function ($args) {
84 4
                return $args;
85 9
            };
86
87 9
            $condition = $this->validatorCallback($modifier, $data);
88 9
            $chain->set('condition', $condition);
89
90 9
            resolve('heyman.chains')->commitChain();
91
        } catch (\Throwable $throwable) {
92
            //
93
        }
94 9
    }
95
96 12
    private function wrapForIgnore($validator)
97
    {
98 12
        return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
99
    }
100
}
101