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

SymfonyContainerMiddlewareChain::lazyLoad()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 7
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 20
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 Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
18
class SymfonyContainerMiddlewareChain implements MiddlewareChain, ContainerAwareInterface
19
{
20
    use ContainerAwareTrait;
21
22
    /**
23
     * @var string[]
24
     */
25
    private $middleware_handler_ids = [];
26
27
    /**
28
     * @param string $service
29
     */
30 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...
31
    {
32
        $index = array_search($service, $this->middleware_handler_ids);
33
34
        // move existing middleware to end of chain
35
        if ($index !== false) {
36
            unset($this->middleware_handler_ids[$index]);
37
        }
38
39
        $this->middleware_handler_ids[] = $service;
40
    }
41
42
    /**
43
     * @param mixed $message
44
     *
45
     * @return mixed
46
     */
47
    public function run($message)
48
    {
49
        return call_user_func($this->call(0), $message);
50
    }
51
52
    /**
53
     * @param int $index
54
     *
55
     * @return \Closure
56
     */
57 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...
58
    {
59
        $middleware = $this->lazyLoad($index);
60
61
        if (!($middleware instanceof MiddlewareHandler)) {
62
            return function ($message) {
63
                return $message;
64
            };
65
        }
66
67
        return function ($message) use ($middleware, $index) {
68
            return $middleware->handle($message, $this->call($index + 1));
69
        };
70
    }
71
72
    /**
73
     * @param $index
74
     *
75
     * @return MiddlewareHandler
76
     */
77 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...
78
    {
79
        if ($this->container instanceof ContainerInterface && isset($this->middleware_handler_ids[$index])) {
80
            $handler = $this->container->get($this->middleware_handler_ids[$index]);
81
82
            if ($handler instanceof MiddlewareHandler) {
83
                return $handler;
84
            }
85
        }
86
87
        return null;
88
    }
89
}
90