Completed
Push — master ( 36764f...e22a75 )
by Iman
03:19
created

ResponderFactory::responseCallback()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 12
ccs 8
cts 8
cp 1
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 56
    public function __construct(Chain $chain)
15
    {
16 56
        $this->chain = $chain;
17 56
    }
18
19 56
    public function make()
20
    {
21 56
        if ($this->chain->abort) {
22 1
            return $this->abortCallback($this->chain->abort);
23 56
        } 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 53
            return $this->exceptionCallback($this->chain->exception);
25 4
        } 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 3
        } 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 3
            return $this->redirectCallback($this->chain->redirect);
29
        }
30
    }
31
32 1
    public function abortCallback($abort)
33
    {
34
        $responder = function () use ($abort) {
35 1
            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

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