BeforeHandling::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 before starting to handle a command.
7
 *
8
 * Used to configure middleware that runs before command handling.
9
 * Such middleware could, for example:
10
 * - fail early if the request is not authenticated
11
 * - log the command handling attempt
12
 * - start a unit of work or database transaction
13
 * - etc
14
 */
15
final class BeforeHandling implements Handler
16
{
17
    private $middleware;
18
    private $handler;
19
20
    private function __construct(Middleware $middleware, Handler $handler)
21
    {
22
        $this->middleware = $middleware;
23
        $this->handler = $handler;
24
    }
25
26
    public static function invoke(
27
        Middleware $middleware,
28
        Handler $handler
29
    ): Handler {
30
        return new self($middleware, $handler);
31
    }
32
33
    /** @inheritdoc */
34
    public function handle(object $command): void
35
    {
36
        $this->middleware->invoke($command);
37
        $this->handler->handle($command);
38
    }
39
}
40