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

ContainerMiddlewareChain   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 45.88 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 39
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 registerService() 13 13 2
A run() 0 4 1
A call() 14 14 2
A lazyLoad() 12 12 3

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 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 2
    public function __construct(ContainerInterface $container)
32
    {
33 2
        $this->container = $container;
34 2
    }
35
36
    /**
37
     * @param string $service
38
     */
39 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...
40
    {
41 1
        $index = array_search($service, $this->middleware_handler_ids);
42
43
        // move existing middleware to end of chain
44 1
        if ($index !== false) {
45 1
            unset($this->middleware_handler_ids[$index]);
46
            // correct array indexes
47 1
            $this->middleware_handler_ids = array_values($this->middleware_handler_ids);
48 1
        }
49
50 1
        $this->middleware_handler_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 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...
69
    {
70 2
        $middleware = $this->lazyLoad($index);
71
72 2
        if (!($middleware instanceof MiddlewareHandler)) {
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 MiddlewareHandler
87
     */
88 2 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...
89
    {
90 2
        if (isset($this->middleware_handler_ids[$index])) {
91 1
            $handler = $this->container->get($this->middleware_handler_ids[$index]);
92
93 1
            if ($handler instanceof MiddlewareHandler) {
94 1
                return $handler;
95
            }
96 1
        }
97
98 2
        return null;
99
    }
100
}
101