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

SymfonyContainerMiddlewareChain   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 51.39 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 37
loc 72
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerService() 11 11 2
A run() 0 4 1
A call() 14 14 2
A lazyLoad() 12 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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