Completed
Pull Request — master (#25)
by Iman
13:26 queued 09:29
created

ResponderFactory::abort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

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

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

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