Completed
Push — master ( e23fd6...395432 )
by Iman
05:06
created

ResponderFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 111
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A exception() 0 6 1
A abort() 0 4 1
A nothing() 0 3 1
A __construct() 0 3 1
A sendResponse() 0 7 2
A validatorCallback() 0 13 2
A respondFrom() 0 4 1
A make() 0 5 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 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
    protected function abort($abort): \Closure
31
    {
32 2
        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
    protected function nothing(): \Closure
38
    {
39 7
        return function () {
40 7
        };
41
    }
42
43
    /**
44
     * @param $e
45
     *
46
     * @return \Closure
47
     */
48
    protected function exception(array $e): \Closure
49
    {
50 81
        return function () use ($e) {
51 40
            $exClass = $e['class'];
52
53 40
            throw new $exClass($e['message']);
54 81
        };
55
    }
56
57
    /**
58
     * @param $resp
59
     *
60
     * @return \Closure
61
     */
62
    protected function response(array $resp): \Closure
63
    {
64 1
        return function () use ($resp) {
65 1
            $this->sendResponse($resp, response());
66 1
        };
67
    }
68
69
    protected function redirect(array $resp): \Closure
70
    {
71 4
        return function () use ($resp) {
72 4
            $this->sendResponse($resp, redirect());
73 4
        };
74
    }
75
76
    protected function respondFrom($method): \Closure
77
    {
78 1
        return function () use ($method) {
79 1
            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 1
        };
81
    }
82
83
    /**
84
     * @param array $resp
85
     * @param $respObj
86
     * @throws \ImanGhafoori\Terminator\TerminateException
87
     */
88 5
    private function sendResponse(array $resp, $respObj)
89
    {
90 5
        foreach ($resp as $call) {
91 5
            list($method, $args) = $call;
92 5
            $respObj = $respObj->{$method}(...$args);
93
        }
94 5
        respondWith($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 = 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

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