ChainFactory::createExecutionChain()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
ccs 0
cts 12
cp 0
crap 20
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Middleware;
16
17
use InvalidArgumentException;
18
19
final class ChainFactory
20
{
21
    /**
22
     * @param Middleware[] $middlewareList
23
     */
24
    public static function createExecutionChain(array $middlewareList, callable $lastCallable): callable
25
    {
26
        while ($middleware = array_pop($middlewareList)) {
27
            if (!$middleware instanceof Middleware) {
28
                throw new InvalidArgumentException(
29
                    sprintf(
30
                        'Middleware must be an instance of %s but %s was given',
31
                        Middleware::class,
32
                        is_object($middleware) ? get_class($middleware) : gettype($middleware)
33
                    )
34
                );
35
            }
36
37
            $lastCallable = function ($command) use ($middleware, $lastCallable) {
38
                return $middleware->execute($command, $lastCallable);
39
            };
40
        }
41
42
        return $lastCallable;
43
    }
44
}
45