Completed
Push — master ( 214ec8...529945 )
by Iman
01:34
created

Proxy::__call()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 15
cts 16
cp 0.9375
rs 9.44
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0039
1
<?php
2
3
namespace Imanghafoori\Middlewarize;
4
5
class Proxy
6
{
7
    private $callable;
8
9
    private $middlewares;
10
11
    /**
12
     * Proxy constructor.
13
     *
14
     * @param $callable
15
     * @param $middlewares
16
     */
17 9
    public function __construct($callable, $middlewares)
18
    {
19 9
        $this->callable = $callable;
20 9
        $this->middlewares = $middlewares;
21 9
    }
22
23 9
    public function __call($method, $params)
24
    {
25 9
        $pipeline = new Pipeline(app());
26
27 9
        if (!is_string($this->callable)) {
28
            $core = (function ($params) use ($method) {
29
                try {
30 4
                    return $this->$method(...$params);
31 1
                } catch (\Throwable $e) {
32 1
                    return $e;
33
                }
34 6
            })->bindTo($this->callable, $this->callable);
35
        } else {
36
            $core = function ($params) use ($method) {
37 1
                return call_user_func_array([$this->callable, $method], $params);
38 3
            };
39
        }
40
41
        $result = $pipeline
42 9
            ->via('handle')
43 9
            ->send($params)
44 9
            ->through($this->middlewares)
45 9
            ->then($core);
46
47 9
        if ($result instanceof \Throwable) {
48
            throw $result;
49
        } else {
50 9
            return $result;
51
        }
52
    }
53
}
54