EventBus::publish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Domain\Eventing;
4
5
use Domain\Eventing\Exception\InvalidMiddlewareException;
6
7
/**
8
 * @author Sebastiaan Hilbers <[email protected]>
9
 */
10
class EventBus
11
{
12
    public function __construct(array $middleware)
13
    {
14
        $this->middlewareChain = $this->createExecutionChain($middleware);
0 ignored issues
show
Bug introduced by
The property middlewareChain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
15
    }
16
17
    public function publish(CommittedEvents $events)
18
    {
19
        var_dump($events);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($events); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
20
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method publish() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
21
    }
22
23
    private function createExecutionChain(array $middlewareList)
24
    {
25
        $lastCallable = function ($command) {
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
            // the final callable is a no-op
27
        };
28
29
        while ($middleware = array_pop($middlewareList)) {
30
            if (! $middleware instanceof Middleware) {
31
                throw InvalidMiddlewareException::forMiddleware($middleware);
32
            }
33
34
            $lastCallable = function ($command) use ($middleware, $lastCallable) {
35
                return $middleware->execute($command, $lastCallable);
36
            };
37
        }
38
39
        return $lastCallable;
40
    }
41
}
42