Completed
Push — master ( cd5a9f...12dfc9 )
by Iman
03:04
created

ResponderFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\HeyMan\Reactions;
4
5
use Imanghafoori\HeyMan\Chain;
6
use Imanghafoori\HeyMan\HeyManSwitcher;
7
8
class ResponderFactory
9
{
10
    private $chain;
11
12
    /**
13
     * ResponderFactory constructor.
14
     *
15
     * @param Chain $chain
16
     */
17 78
    public function __construct(Chain $chain)
18
    {
19 78
        $this->chain = $chain;
20 78
    }
21
22 78
    public function make()
23
    {
24 78
        $m = $this->chain->methodName;
25 78
        $parameters = $this->chain->data;
26
27 78
        return $this->$m($parameters);
28
    }
29
30 2
    public function abort($abort)
31
    {
32
        return function () use ($abort) {
33 2
            abort(...$abort[0]);
0 ignored issues
show
Bug introduced by
$abort[0] 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[0]);
Loading history...
34 2
        };
35
    }
36
37 6
    public function nothing()
38
    {
39
        return function () {
40 6
        };
41
    }
42
43
    /**
44
     * @param $e
45
     *
46
     * @return \Closure
47
     */
48 68
    public function exception($e): \Closure
49
    {
50
        return function () use ($e) {
51 39
            $exClass = $e[0]['class'];
52
53 39
            throw new $exClass($e[0]['message']);
54 68
        };
55
    }
56
57
    /**
58
     * @param $resp
59
     *
60
     * @return \Closure
61
     */
62 1
    public function response($resp): \Closure
63
    {
64
        return function () use ($resp) {
65 1
            $respObj = response();
66 1
            foreach ($resp as $call) {
67 1
                list($method, $args) = $call;
68 1
                $respObj = $respObj->{$method}(...$args);
69
            }
70 1
            respondWith($respObj);
71 1
        };
72
    }
73
74 4
    public function redirect($resp): \Closure
75
    {
76
        return function () use ($resp) {
77 4
            $respObj = redirect();
78 4
            foreach ($resp as $call) {
79 4
                list($method, $args) = $call;
80 4
                $respObj = $respObj->{$method}(...$args);
81
            }
82 4
            respondWith($respObj);
83 4
        };
84
    }
85
86 1
    public function respondFrom($method)
87
    {
88
        return function () use ($method) {
89 1
            respondWith(app()->call(...$method[0]));
0 ignored issues
show
Bug introduced by
It seems like app()->call($method[0]) 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

89
            respondWith(/** @scrutinizer ignore-type */ app()->call(...$method[0]));
Loading history...
Bug introduced by
$method[0] 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

89
            respondWith(app()->call(/** @scrutinizer ignore-type */ ...$method[0]));
Loading history...
90 1
        };
91
    }
92
93 5
    public function validatorCallback($rules): \Closure
94
    {
95
        $validator = function () use ($rules) {
96 2
            if (is_callable($rules)) {
97 1
                $rules = $rules();
98
            }
99 2
            $validator = \Illuminate\Support\Facades\Validator::make(request()->all(), $rules);
100 2
            $validator->validate();
101 5
        };
102
103 5
        return app(HeyManSwitcher::class)->wrapForIgnorance($validator, 'validation');
104
    }
105
}
106