Completed
Push — master ( 94b19c...6780ea )
by Pierre
03:38
created

Middleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 95
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A layer() 0 14 4
A __construct() 0 3 1
A toArray() 0 3 1
A createCoreFunction() 0 4 1
A peel() 0 10 1
A createLayer() 0 4 1
1
<?php
2
3
namespace App\Component\Http;
4
5
use InvalidArgumentException;
6
use Closure;
7
use App\Component\Container;
8
use App\Component\Http\Interfaces\Middleware\ILayer;
9
10
/**
11
 * App\Component\Http\Middleware
12
 *
13
 * is a copy/paste from onion team.
14
 *
15
 * @see https://github.com/esbenp/onion
16
 * @todo Thanks onion team, but composer install fails.
17
 */
18
class Middleware
19
{
20
21
    protected static $excMsg = ' is not a valid onion layer.';
22
    protected $layers;
23
24
    /**
25
     * instanciate
26
     *
27
     * @param array $layers
28
     */
29 11
    public function __construct(array $layers = [])
30
    {
31 11
        $this->layers = $layers;
32
    }
33
34
    /**
35
     * Add layer(s) or Middleware
36
     *
37
     * @param  mixed $layers
38
     * @return Middleware
39
     */
40 2
    public function layer($layers)
41
    {
42 2
        if ($layers instanceof Middleware) {
43 1
            $layers = $layers->toArray();
44
        }
45 2
        if ($layers instanceof ILayer) {
46 1
            $layers = [$layers];
47
        }
48 2
        if (!is_array($layers)) {
49 1
            throw new \InvalidArgumentException(
50 1
                get_class($layers) . self::$excMsg
51
            );
52
        }
53 1
        return new static(array_merge($this->layers, $layers));
54
    }
55
56
    /**
57
     * Run middleware around core function and pass an
58
     * object through it
59
     *
60
     * @param  Container $container
61
     * @param  Closure $core
62
     * @return mixed
63
     */
64 1
    public function peel(Container $object, Closure $core)
65
    {
66 1
        $coreFunction = $this->createCoreFunction($core);
67 1
        $layers = array_reverse($this->layers);
68 1
        $completeMiddleware = array_reduce(
69 1
            $layers,
70 1
            [$this, 'createLayer'],
71 1
            $coreFunction
72
        );
73 1
        return $completeMiddleware($object);
74
    }
75
76
    /**
77
     * Get the layers of this onion,
78
     * can be used to merge with another onion
79
     *
80
     * @return array
81
     */
82 2
    public function toArray(): array
83
    {
84 2
        return $this->layers;
85
    }
86
87
    /**
88
     * The inner function of the onion.
89
     * this will be wrapped on layers
90
     *
91
     * @param  Closure $core the core function
92
     * @return Closure
93
     */
94
    protected function createCoreFunction(Closure $core): Closure
95
    {
96 1
        return function ($object) use ($core) {
97 1
            return $core($object);
98 1
        };
99
    }
100
101
    /**
102
     * Get an onion layer function.
103
     * we get the object from a previous layer and pass it inwards
104
     *
105
     * @param  Closure $nextLayer
106
     * @param  ILayer $layer
107
     * @return Closure
108
     */
109
    protected function createLayer(Closure $nextLayer, ILayer $layer): Closure
110
    {
111 1
        return function ($object) use ($nextLayer, $layer) {
112 1
            return $layer->peel($object, $nextLayer);
113 1
        };
114
    }
115
}
116