Passed
Push — master ( f43d04...c61266 )
by Iman
06:27
created

ResponderFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 11
eloc 29
dl 0
loc 110
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A exception() 0 7 1
A respondFrom() 0 4 1
A abort() 0 4 1
A sendResponse() 0 8 2
A nothing() 0 3 1
A validatorCallback() 0 13 2
A make() 0 6 1
A response() 0 4 1
A redirect() 0 4 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
    public function make()
13
    {
14
        $chain = resolve(ChainManager::class);
15
        $m = $chain->get('responseType');
16
        $data = $chain->get('data');
17
        return $this->$m(...$data);
18 96
    }
19
20 96
    protected function abort($abort): \Closure
21 96
    {
22
        return function () use ($abort) {
23 96
            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
        };
25 96
    }
26
27 96
    protected function nothing(): \Closure
28
    {
29
        return function () {
30
        };
31
    }
32 2
33 2
    /**
34 2
     * @param $e
35
     *
36
     * @return \Closure
37
     */
38
    protected function exception(array $e): \Closure
39 7
    {
40 7
        return function () use ($e) {
41
            $exClass = $e[0];
42
            $message = $e[1];
43
44
            throw new $exClass($message);
45
        };
46
    }
47
48
    /**
49
     * @param $resp
50 82
     *
51 42
     * @return \Closure
52 42
     */
53
    protected function response(...$resp): \Closure
54 42
    {
55 82
        return function () use ($resp) {
56
            $this->sendResponse($resp, response());
57
        };
58
    }
59
60
    protected function redirect(...$resp): \Closure
61
    {
62
        return function () use ($resp) {
63
            $this->sendResponse($resp, redirect());
64
        };
65 1
    }
66 1
67 1
    /**
68
     * @param $method
69
     *
70
     * @throws \Illuminate\Http\Exceptions\HttpResponseException
71
     *
72 6
     * @return \Closure
73 6
     */
74 6
    protected function respondFrom($method): \Closure
75
    {
76
        return function () use ($method) {
77
            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
        };
79 1
    }
80 1
81 1
    /**
82
     * @param array $methodCalls
83
     * @param $respObj
84
     *
85
     * @throws \Illuminate\Http\Exceptions\HttpResponseException
86
     */
87
    private function sendResponse(array $methodCalls, $respObj)
88
    {
89
        foreach ($methodCalls as $call) {
90 7
            list($method, $args) = $call;
91
            $respObj = $respObj->{$method}(...$args);
92 7
        }
93 7
94 7
        throw new HttpResponseException($respObj);
95
    }
96 7
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
        $validator = function () use ($modifier, $rules, $messages, $customAttributes) {
110
            if (is_callable($rules)) {
111 7
                $rules = $rules();
112 4
            }
113 3
114
            $data = app()->call($modifier, [request()->all()]);
115
            $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 4
        };
118 4
119 7
        return resolve(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
120
    }
121
}
122