Passed
Push — master ( 1ff8c3...d57d17 )
by Iman
03:06 queued 10s
created

ResponderFactory::makeValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Illuminate\Contracts\Validation\Factory;
6
use Imanghafoori\HeyMan\Switching\HeyManSwitcher;
7
use Illuminate\Http\Exceptions\HttpResponseException;
8
9
final class ResponderFactory
10
{
11 102
    public function make()
12
    {
13 102
        $chain = resolve('heyman.chain');
14 102
        $method = $chain->get('responseType') ?? 'nothing';
15 102
        $data = $chain->get('data') ?? [];
16
17 102
        return $this->$method(...$data);
18
    }
19
20
    protected function abort($args): \Closure
21
    {
22 2
        return function () use ($args) {
23 2
            abort(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $code of abort() 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

23
            abort(/** @scrutinizer ignore-type */ ...$args);
Loading history...
24 2
        };
25
    }
26
27
    protected function nothing(): \Closure
28
    {
29 8
        return function () {
30 8
        };
31
    }
32
33
    protected function exception(array $e): \Closure
34
    {
35 86
        return function () use ($e) {
36 47
            $exClass = $e[0];
37 47
            $message = $e[1];
38
39 47
            throw new $exClass($message);
40 86
        };
41
    }
42
43
    protected function response(...$resp): \Closure
44
    {
45 2
        return function () use ($resp) {
46 2
            $this->sendResponse($resp, response());
47 2
        };
48
    }
49
50
    protected function redirect(...$resp): \Closure
51
    {
52 7
        return function () use ($resp) {
53 7
            $this->sendResponse($resp, redirect());
54 7
        };
55
    }
56
57
    protected function respondFrom($method): \Closure
58
    {
59 1
        return function () use ($method) {
60 1
            throw new HttpResponseException(app()->call(...$method));
0 ignored issues
show
Bug introduced by
$method is expanded, but the parameter $callback of Illuminate\Container\Container::call() 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

60
            throw new HttpResponseException(app()->call(/** @scrutinizer ignore-type */ ...$method));
Loading history...
Bug introduced by
It seems like app()->call($method) can also be of type callable; however, parameter $response of Illuminate\Http\Exceptio...xception::__construct() does only seem to accept Symfony\Component\HttpFoundation\Response, 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

60
            throw new HttpResponseException(/** @scrutinizer ignore-type */ app()->call(...$method));
Loading history...
61 1
        };
62
    }
63
64 9
    private function sendResponse(array $methodCalls, $respObj)
65
    {
66 9
        foreach ($methodCalls as $call) {
67 9
            [$method, $args] = $call;
68 9
            $respObj = $respObj->{$method}(...$args);
69
        }
70
71 9
        throw new HttpResponseException($respObj);
72
    }
73
74
    public function validatorCallback($modifier, $rules)
75
    {
76 8
        $validator = function () use ($modifier, $rules) {
77 5
            $this->makeValidator($modifier, $rules)->validate();
78 8
        };
79
80 8
        return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
81
    }
82
83
    public function validationPassesCallback($modifier, $rules)
84
    {
85 1
        $validator = function () use ($modifier, $rules) {
86
87 1
            return ! $this->makeValidator($modifier, $rules)->fails();
88 1
        };
89
90 1
        return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
91
    }
92
93 6
    public function makeValidator($modifier, $rules)
94
    {
95 6
        if (is_callable($rules[0])) {
96 4
            $rules[0] = call_user_func($rules[0]);
97
        }
98
99 6
        $newData = app()->call($modifier, [request()->all()]);
100
101 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

101
        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

101
        return resolve(Factory::class)->make(/** @scrutinizer ignore-type */ $newData, ...$rules);
Loading history...
102
    }
103
}
104