Completed
Push — master ( 057913...214ec8 )
by Iman
01:33
created

Proxy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 8
    public function __construct($callable, $middlewares)
18
    {
19 8
        $this->callable = $callable;
20 8
        $this->middlewares = $middlewares;
21 8
    }
22
23 8
    public function __call($method, $params)
24
    {
25 8
        $pipeline = new Pipeline(app());
26
27 8
        if (!is_string($this->callable)) {
28
            $core = (function ($params) use ($method) {
29
                try {
30 3
                    return $this->$method(...$params);
31
                } catch (\Throwable $e) {
32
                    return $e;
33
                }
34 5
            })->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 8
            ->via('handle')
43 8
            ->send($params)
44 8
            ->through($this->middlewares)
45 8
            ->then($core);
46
47 8
        if ($result instanceof \Throwable) {
48
            throw $result;
49
        } else {
50 8
            return $result;
51
        }
52
    }
53
}
54