-
Notifications
You must be signed in to change notification settings - Fork 1
/
PostController.php
73 lines (60 loc) · 1.86 KB
/
PostController.php
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
<?php declare(strict_types=1);
namespace Star\Example\Blog\Application\Http\Controller;
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;
final class PostController
{
/**
* @var CommandBus
*/
private $commandBus;
/**
* @var QueryBus
*/
private $queryBus;
/**
* @var int
*/
private $i = 0;
public function __construct(CommandBus $commandBus, QueryBus $queryBus)
{
$this->commandBus = $commandBus;
$this->queryBus = $queryBus;
}
public function publish(int $postId): string
{
$this->commandBus->dispatchCommand(
new PublishPost(PostId::fromInt($postId), new \DateTime('2000-01-01'), 'username')
);
return \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 \json_encode($query->getResult());
}
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 \json_encode(
[
'id' => $id->toString(),
]
);
}
}