CommandBus   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 97.73 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 43
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A __invoke() 20 20 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
namespace NilPortugues\MessageBus\CommandBus;
4
5
use NilPortugues\Assert\Assert;
6
use NilPortugues\MessageBus\CommandBus\Contracts\Command;
7
use NilPortugues\MessageBus\CommandBus\Contracts\CommandBusMiddleware as CommandBusMiddlewareInterface;
8
9 View Code Duplication
class CommandBus implements CommandBusMiddlewareInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    /** @var callable[] */
12
    protected $middleware = [];
13
14
    /**
15
     * StackedCommandBus constructor.
16
     *
17
     * @param callable[] $middleware
18
     */
19
    public function __construct(array $middleware = [])
20
    {
21
        foreach ($middleware as $commandBusMiddleware) {
22
            Assert::isInstanceOf($commandBusMiddleware, CommandBusMiddlewareInterface::class);
23
        }
24
25
        $this->middleware = $middleware;
26
    }
27
28
    /**
29
     * @param Command       $command
30
     * @param callable|null $next
31
     */
32
    public function __invoke(Command $command, callable $next = null)
33
    {
34
        $middleware = $this->middleware;
35
        $current = array_shift($middleware);
36
37
        if (empty($middleware) && !empty($current)) {
38
            $current->__invoke($command);
0 ignored issues
show
Bug introduced by
The method __invoke cannot be called on $current (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
39
40
            return;
41
        }
42
43
        foreach ($middleware as $commandBusMiddleware) {
44
            $callable = function ($command) use ($commandBusMiddleware) {
45
                return $commandBusMiddleware($command);
46
            };
47
48
            $current->__invoke($command, $callable);
49
            $current = $commandBusMiddleware;
50
        }
51
    }
52
}
53