Passed
Push — master ( 405c7b...2465e3 )
by Iman
11:52 queued 08:43
created

ResponderFactory::sendResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
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 Illuminate\Http\Exceptions\HttpResponseException;
7
use Imanghafoori\HeyMan\Switching\HeyManSwitcher;
8
9
final class ResponderFactory
10
{
11 96
    public function make()
12
    {
13 96
        $chain = resolve('heyman.chain');
14 96
        $m = $chain->get('responseType') ?? 'nothing';
15 96
        $data = $chain->get('data') ?? [];
16
17 96
        return $this->$m(...$data);
18
    }
19
20
    protected function abort($abort): \Closure
21
    {
22 2
        return function () use ($abort) {
23 2
            abort(...$abort);
0 ignored issues
show
Bug introduced by
$abort 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 */ ...$abort);
Loading history...
24 2
        };
25
    }
26
27
    protected function nothing(): \Closure
28
    {
29 8
        return function () {
30 8
        };
31
    }
32
33
    /**
34
     * @param $e
35
     *
36
     * @return \Closure
37
     */
38
    protected function exception(array $e): \Closure
39
    {
40 82
        return function () use ($e) {
41 42
            $exClass = $e[0];
42 42
            $message = $e[1];
43
44 42
            throw new $exClass($message);
45 82
        };
46
    }
47
48
    /**
49
     * @param $resp
50
     *
51
     * @return \Closure
52
     */
53
    protected function response(...$resp): \Closure
54
    {
55 1
        return function () use ($resp) {
56 1
            $this->sendResponse($resp, response());
57 1
        };
58
    }
59
60
    protected function redirect(...$resp): \Closure
61
    {
62 6
        return function () use ($resp) {
63 6
            $this->sendResponse($resp, redirect());
64 6
        };
65
    }
66
67
    /**
68
     * @param $method
69
     *
70
     * @throws \Illuminate\Http\Exceptions\HttpResponseException
71
     *
72
     * @return \Closure
73
     */
74
    protected function respondFrom($method): \Closure
75
    {
76 1
        return function () use ($method) {
77 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

77
            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

77
            throw new HttpResponseException(/** @scrutinizer ignore-type */ app()->call(...$method));
Loading history...
78 1
        };
79
    }
80
81
    /**
82
     * @param array $methodCalls
83
     * @param $respObj
84
     *
85
     * @throws \Illuminate\Http\Exceptions\HttpResponseException
86
     */
87 7
    private function sendResponse(array $methodCalls, $respObj)
88
    {
89 7
        foreach ($methodCalls as $call) {
90 7
            list($method, $args) = $call;
91 7
            $respObj = $respObj->{$method}(...$args);
92
        }
93
94 7
        throw new HttpResponseException($respObj);
95
    }
96
97
    /**
98
     * Validate the given request with the given rules.
99
     *
100
     * @param string|\Closure $modifier
101
     * @param array|\Closure  $rules
102
     * @param array           $messages
103
     * @param array           $customAttributes
104
     *
105
     * @return \Closure
106
     */
107
    public function validatorCallback($modifier, $rules, array $messages = [], array $customAttributes = []): \Closure
108
    {
109 7
        $validator = function () use ($modifier, $rules, $messages, $customAttributes) {
110 4
            if (is_callable($rules)) {
111 3
                $rules = $rules();
112
            }
113
114 4
            $data = app()->call($modifier, [request()->all()]);
115 4
            $validator = resolve(Factory::class)->make($data, $rules, $messages, $customAttributes);
0 ignored issues
show
Bug introduced by
It seems like $data 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

115
            $validator = resolve(Factory::class)->make(/** @scrutinizer ignore-type */ $data, $rules, $messages, $customAttributes);
Loading history...
116 4
            $validator->validate();
117 7
        };
118
119 7
        return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
120
    }
121
}
122