Passed
Push — master ( 8a1a57...1ff8c3 )
by Iman
03:17
created

Validator::__construct()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 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 1
        $validator = function () use ($modifier, $rules) {
27 1
            if (is_callable($rules[0])) {
28
                $rules[0] = call_user_func($rules[0]);
29
            }
30
31 1
            $newData = app()->call($modifier, [request()->all()]);
32 1
            $validator = 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

32
            $validator = 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

32
            $validator = resolve(Factory::class)->make(/** @scrutinizer ignore-type */ $newData, ...$rules);
Loading history...
33
34 1
            return ! $validator->fails();
35 1
        };
36
37 1
        $result = resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
38
39 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

39
        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...
40
41 1
        return resolve(Reaction::class);
42
    }
43
44 1
    public function beforeValidationModifyData($callable)
45
    {
46 1
        $this->modifier = $callable;
47 1
    }
48
49 8
    public function __destruct()
50
    {
51 8
        $data = $this->validationData;
52 8
        $modifier = $this->modifier ?: function ($args) {
53 4
            return $args;
54 8
        };
55
56
        try {
57 8
            $chain = app('heyman.chain');
58 8
            $condition = $chain->get('condition');
59 8
            if (! $condition) {
60 8
                $condition = resolve(ResponderFactory::class)->validatorCallback($modifier, $data);
61 8
                $chain->set('condition', $condition);
62
            }
63 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

63
            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...
64
        } catch (\Throwable $throwable) {
65
            //
66
        }
67 8
    }
68
}
69