-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostController.php
More file actions
75 lines (64 loc) · 1.96 KB
/
Copy pathPostController.php
File metadata and controls
75 lines (64 loc) · 1.96 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php declare(strict_types=1);
namespace Star\Example\Blog\Application\Http\Controller;
use DateTime;
use Star\Example\Blog\Domain\Command\Post\CreateNewPost;
use Star\Component\DomainEvent\Messaging\CommandBus;
use Star\Component\DomainEvent\Messaging\QueryBus;
use Star\Example\Blog\Domain\Command\Post\PublishPost;
use Star\Example\Blog\Domain\Model\BlogId;
use Star\Example\Blog\Domain\Model\Post\PostId;
use Star\Example\Blog\Domain\Model\Post\PostTitle;
use Star\Example\Blog\Domain\Query\Post\SearchForPost;
use function implode;
use function json_encode;
use function sprintf;
final class PostController
{
private int $i = 0;
public function __construct(
private CommandBus $commandBus,
private QueryBus $queryBus,
) {
}
public function publish(int $postId): string
{
$this->commandBus->dispatchCommand(
new PublishPost(PostId::fromInt($postId), new DateTime('2000-01-01'), 'username')
);
return (string) json_encode(['success' => true]);
}
public function search(string ...$patterns): string
{
echo sprintf('Searching: %s.', implode(', ', $patterns)) . "\n";
$query = new SearchForPost(...$patterns);
$this->queryBus->dispatchQuery($query);
return (string) json_encode($query->getResult());
}
/**
* @param string $blogId
* @param array{
* data: array{
* title: string,
* },
* } $request
* @return string
*/
public function createPost(
string $blogId,
array $request,
): string {
$this->i ++;
$this->commandBus->dispatchCommand(
new CreateNewPost(
$id = PostId::fromString((string) $this->i),
new PostTitle($request['data']['title']),
new BlogId($blogId)
)
);
return (string) json_encode(
[
'id' => $id->toString(),
]
);
}
}