1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace SmoothPhp\LaravelAdapter\EventBus; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Contracts\Queue\Queue; |
7
|
|
|
use SmoothPhp\Contracts\Domain\DomainMessage; |
8
|
|
|
use SmoothPhp\Contracts\EventBus\EventListener; |
9
|
|
|
use SmoothPhp\Contracts\Serialization\Serializer; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PushEventsThroughQueue |
13
|
|
|
* @package SmoothPhp\LaravelAdapter\EventBus |
14
|
|
|
* @author Simon Bennett <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class PushEventsThroughQueue implements EventListener |
17
|
|
|
{ |
18
|
|
|
/** @var Queue */ |
19
|
|
|
private $queue; |
20
|
|
|
|
21
|
|
|
/** @var Serializer */ |
22
|
|
|
private $serializer; |
23
|
|
|
/** @var Repository */ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* PushEventsThroughQueue constructor. |
28
|
|
|
* @param Queue $queue |
29
|
|
|
* @param Serializer $serializer |
30
|
|
|
* @param Repository $config |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Queue $queue, Serializer $serializer, Repository $config) |
33
|
|
|
{ |
34
|
|
|
$this->queue = $queue; |
35
|
|
|
$this->serializer = $serializer; |
36
|
|
|
$this->config = $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param DomainMessage $domainMessage |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
public function handle(DomainMessage $domainMessage) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this->queue->push( |
46
|
|
|
QueueToEventDispatcher::class, |
47
|
|
|
[ |
48
|
|
|
'uuid' => (string)$domainMessage->getId(), |
49
|
|
|
'playhead' => $domainMessage->getPlayHead(), |
50
|
|
|
'metadata' => json_encode($this->serializer->serialize($domainMessage->getMetadata())), |
51
|
|
|
'payload' => json_encode($this->serializer->serialize($domainMessage->getPayload())), |
52
|
|
|
'recorded_on' => $domainMessage->getRecordedOn()->format('Y-m-d H:i:s'), |
53
|
|
|
'type' => $domainMessage->getType(), |
54
|
|
|
], |
55
|
|
|
$this->config->get('cqrses.queue_name', 'default') |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.