AfterHandling::invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\CommandHandling;
4
5
/**
6
 * Decorator that invokes the middleware after successfully handling a command.
7
 *
8
 * Used to configure middleware that runs after command handling.
9
 * Such middleware could, for example:
10
 * - log the successful processing of the command
11
 * - save the changes to the data store
12
 * - etc
13
 */
14
final class AfterHandling implements Handler
15
{
16
    private $middleware;
17
    private $handler;
18
19
    private function __construct(Middleware $middleware, Handler $handler)
20
    {
21
        $this->middleware = $middleware;
22
        $this->handler = $handler;
23
    }
24
25
    public static function invoke(
26
        Middleware $middleware,
27
        Handler $handler
28
    ): Handler {
29
        return new self($middleware, $handler);
30
    }
31
32
    /** @inheritdoc */
33
    public function handle(object $command): void
34
    {
35
        $this->handler->handle($command);
36
        $this->middleware->invoke($command);
37
    }
38
}
39