Passed
Push — master ( 685e2a...9f4f72 )
by Iman
03:13
created

Response::sendResponse()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Imanghafoori\HeyMan\Plugins\Reactions;
4
5
use Imanghafoori\HeyMan\Core\BaseReaction;
6
use Imanghafoori\HeyMan\Reactions\Responder;
7
use Illuminate\Http\Exceptions\HttpResponseException;
8
use Imanghafoori\HeyMan\Plugins\Reactions\Redirect\Redirector;
9
10
class Response extends BaseReaction
11
{
12 3
    public function response()
13
    {
14 3
        resolve('heyman.chain')->set('responseType', [static::class, 'respond']);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
        resolve('heyman.chain')->/** @scrutinizer ignore-call */ set('responseType', [static::class, 'respond']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
15
16 3
        return new Responder($this);
17
    }
18
19 2
    public static function respond(...$resp)
20
    {
21 2
        return self::sendResponse($resp, 'response');
22
    }
23
24 8
    public function redirect()
25
    {
26 8
        resolve('heyman.chain')->set('responseType', [static::class, 'makeRedirect']);
27
28 8
        return new Redirector($this);
29
    }
30
31 7
    public static function makeRedirect(...$resp)
32
    {
33 7
        return self::sendResponse($resp, 'redirect');
34
    }
35
36
    private static function sendResponse(array $methodCalls, $func)
37
    {
38 9
        return function () use ($func, $methodCalls) {
39 9
            $respObj = $func();
40 9
            foreach ($methodCalls as $call) {
41 9
                [$method, $args] = $call;
42 9
                $respObj = $respObj->{$method}(...$args);
43
            }
44
45 9
            throw new HttpResponseException($respObj);
46 9
        };
47
    }
48
}