ContainerMiddlewareChain   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 85
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 4 1
A registerService() 0 13 2
A call() 0 14 2
A lazyLoad() 0 12 3
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Middleware\Chain;
12
13
use GpsLab\Component\Middleware\Middleware;
14
use Psr\Container\ContainerInterface;
15
16
class ContainerMiddlewareChain implements MiddlewareChain
17
{
18
    /**
19
     * @var ContainerInterface
20
     */
21
    private $container;
22
23
    /**
24
     * @var string[]
25
     */
26
    private $middleware_ids = [];
27
28
    /**
29
     * @param ContainerInterface $container
30
     */
31 2
    public function __construct(ContainerInterface $container)
32
    {
33 2
        $this->container = $container;
34 2
    }
35
36
    /**
37
     * @param string $service
38
     */
39 1
    public function registerService($service)
40
    {
41 1
        $index = array_search($service, $this->middleware_ids);
42
43
        // move existing middleware to end of chain
44 1
        if ($index !== false) {
45 1
            unset($this->middleware_ids[$index]);
46
            // correct array indexes
47 1
            $this->middleware_ids = array_values($this->middleware_ids);
48 1
        }
49
50 1
        $this->middleware_ids[] = $service;
51 1
    }
52
53
    /**
54
     * @param mixed $message
55
     *
56
     * @return mixed
57
     */
58 2
    public function run($message)
59
    {
60 2
        return call_user_func($this->call(0), $message);
61
    }
62
63
    /**
64
     * @param int $index
65
     *
66
     * @return callable
67
     */
68 2
    private function call($index)
69
    {
70 2
        $middleware = $this->lazyLoad($index);
71
72 2
        if (!($middleware instanceof Middleware)) {
73
            return function ($message) {
74 2
                return $message;
75 2
            };
76
        }
77
78 1
        return function ($message) use ($middleware, $index) {
79 1
            return $middleware->handle($message, $this->call($index + 1));
80 1
        };
81
    }
82
83
    /**
84
     * @param $index
85
     *
86
     * @return Middleware
87
     */
88 2
    private function lazyLoad($index)
89
    {
90 2
        if (isset($this->middleware_ids[$index])) {
91 1
            $middleware = $this->container->get($this->middleware_ids[$index]);
92
93 1
            if ($middleware instanceof Middleware) {
94 1
                return $middleware;
95
            }
96 1
        }
97
98 2
        return null;
99
    }
100
}
101