Completed
Push — master ( ad0dc3...27a97f )
by Iman
07:58
created

ResponderFactory::sendResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 3
cts 3
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 93
        $parameters = $this->chain->data;
27
28 93
        return $this->$m($parameters);
29
    }
30
31
    protected function abort($abort): \Closure
32
    {
33 2
        return function () use ($abort) {
34 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

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

79
            respondWith(/** @scrutinizer ignore-type */ app()->call(...$method));
Loading history...
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

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

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