Completed
Push — master ( c8c6b4...4b9fff )
by Iman
09:32 queued 06:06
created

ResponderFactory::sendResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Illuminate\Contracts\Validation\Factory;
6
use Imanghafoori\HeyMan\Chain;
7
use Imanghafoori\HeyMan\HeyManSwitcher;
8
9
class ResponderFactory
10
{
11
    private $chain;
12
13
    /**
14
     * ResponderFactory constructor.
15
     *
16
     * @param Chain $chain
17
     */
18 93
    public function __construct(Chain $chain)
19
    {
20 93
        $this->chain = $chain;
21 93
    }
22
23 93
    public function make()
24
    {
25 93
        $m = $this->chain->responseType;
26
27 93
        return $this->$m($this->chain->data);
28
    }
29
30 2
    protected function abort($abort): \Closure
31
    {
32
        return function () use ($abort) {
33 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

33
            abort(/** @scrutinizer ignore-type */ ...$abort);
Loading history...
34 2
        };
35
    }
36
37 7
    protected function nothing(): \Closure
38
    {
39
        return function () {
40 7
        };
41
    }
42
43
    /**
44
     * @param $e
45
     *
46
     * @return \Closure
47
     */
48 81
    protected function exception(array $e): \Closure
49
    {
50
        return function () use ($e) {
51 40
            $exClass = $e['class'];
52 40
            $message = $e['message'];
53
54 40
            throw new $exClass($message);
55 81
        };
56
    }
57
58
    /**
59
     * @param $resp
60
     *
61
     * @return \Closure
62
     */
63 1
    protected function response(array $resp): \Closure
64
    {
65
        return function () use ($resp) {
66 1
            $this->sendResponse($resp, response());
67 1
        };
68
    }
69
70 4
    protected function redirect(array $resp): \Closure
71
    {
72
        return function () use ($resp) {
73 4
            $this->sendResponse($resp, redirect());
74 4
        };
75
    }
76
77 1
    protected function respondFrom($method): \Closure
78
    {
79
        return function () use ($method) {
80 1
            respondWith(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

80
            respondWith(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 respondWith() does only seem to accept string, 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

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

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