-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandHandler.php
More file actions
42 lines (32 loc) · 1.17 KB
/
CommandHandler.php
File metadata and controls
42 lines (32 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/*
* Copyright 2025 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace CloudCreativity\Modules\Application\Bus;
use CloudCreativity\Modules\Application\Messages\HandlesMessages;
use CloudCreativity\Modules\Contracts\Application\Bus\CommandHandler as ICommandHandler;
use CloudCreativity\Modules\Contracts\Toolkit\Messages\Command;
use CloudCreativity\Modules\Contracts\Toolkit\Result\Result;
final readonly class CommandHandler implements ICommandHandler
{
use HandlesMessages;
public function __construct(private object $handler)
{
}
public function __invoke(Command $command): Result
{
assert(method_exists($this->handler, 'execute'), sprintf(
'Cannot dispatch "%s" - handler "%s" does not have an execute method.',
$command::class,
$this->handler::class,
));
$result = $this->handler->execute($command);
assert($result instanceof Result, 'Expecting command handler to return a result.');
return $result;
}
}