Completed
Push — master ( fabbb1...4f02ff )
by Iman
01:35
created

Proxy   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 20 2
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 5
    public function __construct($callable, $middlewares)
20
    {
21 5
        $this->callable = $callable;
22 5
        $this->middlewares = $middlewares;
23 5
    }
24
25 5
    public function __call($method, $params)
26
    {
27 5
        $pipeline = new Pipeline(app());
28
        
29 5
        if (!is_string($this->callable)) {
30
            $core = (function ($params) use ($method) {
31 1
                return $this->$method(...$params);
32 3
            })->bindTo($this->callable, $this->callable);
33
        } else {
34
            $core = function ($params) use ($method) {
35 1
                return call_user_func_array([$this->callable, $method], $params);
36 2
            };
37
        }
38
39
        return $pipeline
40 5
            ->via('handle')
41 5
            ->send($params)
42 5
            ->through($this->middlewares)
43 5
            ->then($core);
44
    }
45
}
46