Completed
Push — master ( 67a91e...657487 )
by Peter
06:52
created

ContainerMiddlewareChain::lazyLoad()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 12
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\Handler\MiddlewareHandler;
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_handler_ids = [];
27
28
    /**
29
     * @param ContainerInterface $container
30
     */
31
    public function __construct(ContainerInterface $container)
32
    {
33
        $this->container = $container;
34
    }
35
36
    /**
37
     * @param string $service
38
     */
39 View Code Duplication
    public function registerService($service)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $index = array_search($service, $this->middleware_handler_ids);
42
43
        // move existing middleware to end of chain
44
        if ($index !== false) {
45
            unset($this->middleware_handler_ids[$index]);
46
        }
47
48
        $this->middleware_handler_ids[] = $service;
49
    }
50
51
    /**
52
     * @param mixed $message
53
     *
54
     * @return mixed
55
     */
56
    public function run($message)
57
    {
58
        return call_user_func($this->call(0), $message);
59
    }
60
61
    /**
62
     * @param int $index
63
     *
64
     * @return \Closure
65
     */
66 View Code Duplication
    private function call($index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $middleware = $this->lazyLoad($index);
69
70
        if (!($middleware instanceof MiddlewareHandler)) {
71
            return function ($message) {
72
                return $message;
73
            };
74
        }
75
76
        return function ($message) use ($middleware, $index) {
77
            return $middleware->handle($message, $this->call($index + 1));
78
        };
79
    }
80
81
    /**
82
     * @param $index
83
     *
84
     * @return MiddlewareHandler
85
     */
86 View Code Duplication
    private function lazyLoad($index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        if (isset($this->middleware_handler_ids[$index])) {
89
            $handler = $this->container->get($this->middleware_handler_ids[$index]);
90
91
            if ($handler instanceof MiddlewareHandler) {
92
                return $handler;
93
            }
94
        }
95
96
        return null;
97
    }
98
}
99