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

Proxy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 49
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 30 4
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