Passed
Pull Request — master (#73)
by Iman
08:53 queued 06:15
created

ResponderFactory::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\ChainManager;
8
use Imanghafoori\HeyMan\Switching\HeyManSwitcher;
9
10
final class ResponderFactory
11
{
12 96
    public function make()
13
    {
14 96
        $chain = resolve(ChainManager::class);
15 96
        $m = $chain->get('responseType');
16 96
        $data = $chain->get('data');
17
18 96
        return $this->$m(...$data);
19
    }
20
21
    protected function abort($abort): \Closure
22
    {
23 2
        return function () use ($abort) {
24 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

24
            abort(/** @scrutinizer ignore-type */ ...$abort);
Loading history...
25 2
        };
26
    }
27
28
    protected function nothing(): \Closure
29
    {
30 8
        return function () {
31 8
        };
32
    }
33
34
    /**
35
     * @param $e
36
     *
37
     * @return \Closure
38
     */
39
    protected function exception(array $e): \Closure
40
    {
41 82
        return function () use ($e) {
42 42
            $exClass = $e[0];
43 42
            $message = $e[1];
44
45 42
            throw new $exClass($message);
46 82
        };
47
    }
48
49
    /**
50
     * @param $resp
51
     *
52
     * @return \Closure
53
     */
54
    protected function response(...$resp): \Closure
55
    {
56 1
        return function () use ($resp) {
57 1
            $this->sendResponse($resp, response());
58 1
        };
59
    }
60
61
    protected function redirect(...$resp): \Closure
62
    {
63 6
        return function () use ($resp) {
64 6
            $this->sendResponse($resp, redirect());
65 6
        };
66
    }
67
68
    /**
69
     * @param $method
70
     *
71
     * @throws \Illuminate\Http\Exceptions\HttpResponseException
72
     *
73
     * @return \Closure
74
     */
75
    protected function respondFrom($method): \Closure
76
    {
77 1
        return function () use ($method) {
78 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

78
            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

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

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