Completed
Push — master ( 657487...fd235e )
by Peter
03:08
created

SymfonyContainerMiddlewareChain::call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
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 1 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 1
        $index = array_search($service, $this->middleware_handler_ids);
33
34
        // move existing middleware to end of chain
35 1
        if ($index !== false) {
36 1
            unset($this->middleware_handler_ids[$index]);
37
            // correct array indexes
38 1
            $this->middleware_handler_ids = array_values($this->middleware_handler_ids);
39 1
        }
40
41 1
        $this->middleware_handler_ids[] = $service;
42 1
    }
43
44
    /**
45
     * @param mixed $message
46
     *
47
     * @return mixed
48
     */
49 3
    public function run($message)
50
    {
51 3
        return call_user_func($this->call(0), $message);
52
    }
53
54
    /**
55
     * @param int $index
56
     *
57
     * @return callable
58
     */
59 3 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...
60
    {
61 3
        $middleware = $this->lazyLoad($index);
62
63 3
        if (!($middleware instanceof MiddlewareHandler)) {
64
            return function ($message) {
65 3
                return $message;
66 3
            };
67
        }
68
69 1
        return function ($message) use ($middleware, $index) {
70 1
            return $middleware->handle($message, $this->call($index + 1));
71 1
        };
72
    }
73
74
    /**
75
     * @param $index
76
     *
77
     * @return MiddlewareHandler
78
     */
79 3 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...
80
    {
81 3
        if ($this->container instanceof ContainerInterface && isset($this->middleware_handler_ids[$index])) {
82 1
            $handler = $this->container->get($this->middleware_handler_ids[$index]);
83
84 1
            if ($handler instanceof MiddlewareHandler) {
85 1
                return $handler;
86
            }
87 1
        }
88
89 3
        return null;
90
    }
91
}
92