Completed
Push — master ( 9a00a2...b256dc )
by Iman
02:07
created

Proxy::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.024

Importance

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