Passed
Branch master (28f442)
by Pierre
09:11 queued 06:31
created

Middleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 92.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 97
ccs 25
cts 27
cp 0.9259
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A layer() 0 14 4
A createCoreFunction() 0 4 1
A toArray() 0 3 1
A createLayer() 0 4 1
A peel() 0 12 1
1
<?php
2
3
namespace App\Http;
4
5
use InvalidArgumentException;
6
use Closure;
7
use App\Container;
8
use App\Http\Interfaces\Middleware\ILayer;
9
10
/**
11
 * App\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 10
    public function __construct(array $layers = [])
30
    {
31 10
        $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
            function ($nextLayer, $layer) {
71
                return $this->createLayer($nextLayer, $layer);
72 1
            },
73
            $coreFunction
74
        );
75 1
        return $completeMiddleware($object);
76
    }
77
78
    /**
79
     * Get the layers of this onion,
80
     * can be used to merge with another onion
81
     *
82
     * @return array
83
     */
84 2
    public function toArray(): array
85
    {
86 2
        return $this->layers;
87
    }
88
89
    /**
90
     * The inner function of the onion.
91
     * this will be wrapped on layers
92
     *
93
     * @param  Closure $core the core function
94
     * @return Closure
95
     */
96
    protected function createCoreFunction(Closure $core): Closure
97
    {
98 1
        return function ($object) use ($core) {
99 1
            return $core($object);
100 1
        };
101
    }
102
103
    /**
104
     * Get an onion layer function.
105
     * we get the object from a previous layer and pass it inwards
106
     *
107
     * @param  Closure $nextLayer
108
     * @param  ILayer $layer
109
     * @return Closure
110
     */
111
    protected function createLayer(Closure $nextLayer, ILayer $layer): Closure
112
    {
113 1
        return function ($object) use ($nextLayer, $layer) {
114
            return $layer->peel($object, $nextLayer);
115 1
        };
116
    }
117
}
118