|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Core\Component\Blog\Application\Service; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\Blog\Domain\Post\Comment\Comment; |
|
18
|
|
|
use Acme\App\Core\Component\Blog\Domain\Post\Post; |
|
19
|
|
|
use Acme\App\Core\Port\EventDispatcher\EventDispatcherInterface; |
|
20
|
|
|
use Acme\App\Core\SharedKernel\Component\Blog\Application\Event\CommentCreatedEvent; |
|
21
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
|
22
|
|
|
|
|
23
|
|
|
final class CommentService |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var EventDispatcherInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $eventDispatcher; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function create(Post $post, Comment $comment, UserId $authorId): void |
|
36
|
|
|
{ |
|
37
|
|
|
$comment->setAuthorId($authorId); |
|
38
|
|
|
$post->addComment($comment); |
|
39
|
|
|
|
|
40
|
|
|
// When triggering an event, you can optionally pass some information. |
|
41
|
|
|
// For simple applications, use the GenericEvent object provided by Symfony |
|
42
|
|
|
// to pass some PHP variables. For more complex applications, define your |
|
43
|
|
|
// own event object classes. |
|
44
|
|
|
// See https://symfony.com/doc/current/components/event_dispatcher/generic_event.html |
|
45
|
|
|
$event = new CommentCreatedEvent($comment->getId()); |
|
46
|
|
|
|
|
47
|
|
|
// When an event is dispatched, Symfony notifies it to all the listeners |
|
48
|
|
|
// and subscribers registered to it. Listeners can modify the information |
|
49
|
|
|
// passed in the event and they can even modify the execution flow, so |
|
50
|
|
|
// there's no guarantee that the rest of this method will be executed. |
|
51
|
|
|
// See https://symfony.com/doc/current/components/event_dispatcher.html |
|
52
|
|
|
$this->eventDispatcher->dispatch($event); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|