Passed
Push — master ( e45fc8...78d172 )
by Iman
03:49
created

Validator::getModifier()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Illuminate\Contracts\Validation\Factory;
6
use Imanghafoori\HeyMan\Core\Reaction;
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
        $result = $this->validationPassesCallback($this->getModifier(), $rules);
24
25 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

25
        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...
26
27 3
        return resolve(Reaction::class);
28
    }
29
30 1
    public function beforeValidationModifyData($callable)
31
    {
32 1
        $this->modifier = $callable;
33 1
    }
34
35
    public function validatorCallback($modifier, $rules)
36
    {
37 9
        $validator = function () use ($modifier, $rules) {
38 5
            $this->makeValidator($modifier, $rules)->validate();
39 9
        };
40
41 9
        return $this->wrapForIgnore($validator);
42
    }
43
44
    public function validationPassesCallback($modifier, $rules)
45
    {
46 3
        $validator = function () use ($modifier, $rules) {
47 1
            $v = $this->makeValidator($modifier, $rules);
48
49 1
            resolve('heyman.chain')->set('condition_meta', $v);
50
51 1
            return ! $v->fails();
52 3
        };
53
54 3
        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 10
    public function __destruct()
69
    {
70
        try {
71 10
            $chain = app('heyman.chain');
72 10
            $condition = $chain->get('condition');
73
74 10
            if ($condition) {
75 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

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