ContainerAwareSelfExecutionMiddleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 33
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 10 3
1
<?php
2
namespace Spekkionu\Tactician\SelfExecuting;
3
4
use League\Container\Container;
5
use League\Tactician\Middleware;
6
7
/**
8
 * Middleware are the plugins of Tactician. They receive each command that's
9
 * given to the CommandBus and can take any action they choose. Middleware can
10
 * continue the Command processing by passing the command they receive to the
11
 * $next callable, which is essentially the "next" Middleware in the chain.
12
 *
13
 * Depending on where they invoke the $next callable, Middleware can execute
14
 * their custom logic before or after the Command is handled. They can also
15
 * modify, log, or replace the command they receive. The sky's the limit.
16
 */
17
class ContainerAwareSelfExecutionMiddleware implements Middleware
18
{
19
    /**
20
     * @var Container
21
     */
22
    private $container;
23
24
    /**
25
     * SelfExecutionMiddleware constructor.
26
     * @param Container $container
27
     */
28 4
    public function __construct(Container $container)
29
    {
30 4
        $this->container = $container;
31 4
    }
32
33
    /**
34
     * @param object $command
35
     * @param callable $next
36
     * @return mixed
37
     * @throws CommandHasNoHandleMethodException
38
     */
39 4
    public function execute($command, callable $next)
40
    {
41 4
        if (!$command instanceof SelfExecutingCommand) {
42 1
            return $next($command);
43
        }
44 3
        if (!method_exists($command, 'handle')) {
45 1
            throw new CommandHasNoHandleMethodException('Command does not have handle method');
46
        }
47 2
        return $this->container->call([$command, 'handle']);
48
    }
49
}
50