Completed
Push — master ( c6b6d5...70eeeb )
by Iman
06:33
created

ResponderFactory::respondFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
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 75
    public function __construct(Chain $chain)
15
    {
16 75
        $this->chain = $chain;
17 75
    }
18
19 75
    public function make()
20
    {
21 75
        if ($this->chain->abort) {
22 2
            return $this->abortCallback($this->chain->abort);
23 74
        } elseif ($this->chain->exception) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->chain->exception of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
24 68
            return $this->exceptionCallback($this->chain->exception);
25 9
        } elseif ($this->chain->response) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->chain->response of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
26 1
            return $this->responseCallback($this->chain->response);
27 8
        } elseif ($this->chain->redirect) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->chain->redirect of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
28 4
            return $this->redirectCallback($this->chain->redirect);
29
        } elseif ($this->chain->respondFrom) {
30
            return $this->respondFrom($this->chain->respondFrom);
31 4
        } else {
32
            return function () {
33
            };
34
        }
35 2
    }
36
37
    public function abortCallback($abort)
38 2
    {
39 2
        $responder = function () use ($abort) {
40
            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

40
            abort(/** @scrutinizer ignore-type */ ...$abort);
Loading history...
41 2
        };
42
43
        return $responder;
44
    }
45
46
    /**
47
     * @param $e
48
     * @param $cb
49
     *
50 68
     * @return \Closure
51
     */
52
    public function exceptionCallback($e): \Closure
53 39
    {
54
        $responder = function () use ($e) {
55 39
            $exClass = $e['class'];
56 68
57
            throw new $exClass($e['message']);
58 68
        };
59
60
        return $responder;
61
    }
62
63
    /**
64
     * @param $resp
65
     *
66 1
     * @return \Closure
67
     */
68
    public function responseCallback($resp): \Closure
69 1
    {
70 1
        $responder = function () use ($resp) {
71 1
            $respObj = response();
72 1
            foreach ($resp as $call) {
73
                list($method, $args) = $call;
74 1
                $respObj = $respObj->{$method}(...$args);
75 1
            }
76
            respondWith($respObj);
77 1
        };
78
79
        return $responder;
80 4
    }
81
82
    public function redirectCallback($resp): \Closure
83 4
    {
84 4
        $responder = function () use ($resp) {
85 4
            $respObj = redirect();
86 4
            foreach ($resp as $call) {
87
                list($method, $args) = $call;
88 4
                $respObj = $respObj->{$method}(...$args);
89 4
            }
90
            respondWith($respObj);
91 4
        };
92
93
        return $responder;
94
    }
95
96
    public function respondFrom($method)
97
    {
98
        return function () use ($method) {
99
            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

99
            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

99
            respondWith(/** @scrutinizer ignore-type */ app()->call(...$method));
Loading history...
100
        };
101
    }
102
}
103