1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\Job\Application\Messaging; |
4
|
|
|
|
5
|
|
|
use Interop\Queue\PsrContext; |
6
|
|
|
use Interop\Queue\PsrMessage; |
7
|
|
|
use Interop\Queue\PsrProducer; |
8
|
|
|
use Interop\Queue\PsrTopic; |
9
|
|
|
use Shippinno\Job\Domain\Model\FailedToEnqueueStoredJobException; |
10
|
|
|
use Shippinno\Job\Domain\Model\JobStore; |
11
|
|
|
use Shippinno\Job\Domain\Model\StoredJob; |
12
|
|
|
use Shippinno\Job\Domain\Model\StoredJobNotFoundException; |
13
|
|
|
use Shippinno\Job\Domain\Model\StoredJobSerializer; |
14
|
|
|
use Throwable; |
15
|
|
|
|
16
|
|
|
class EnqueueSingleStoredJobService |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var PsrContext |
20
|
|
|
*/ |
21
|
|
|
private $context; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var JobStore |
25
|
|
|
*/ |
26
|
|
|
protected $jobStore; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var StoredJobSerializer |
30
|
|
|
*/ |
31
|
|
|
private $storedJobSerializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var JobFlightManager |
35
|
|
|
*/ |
36
|
|
|
private $jobFlightManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param PsrContext $context |
40
|
|
|
* @param JobStore $jobStore |
41
|
|
|
* @param StoredJobSerializer $storedJobSerializer |
42
|
|
|
* @param JobFlightManager|null $jobFlightManager |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
45
|
|
|
PsrContext $context, |
46
|
|
|
JobStore $jobStore, |
47
|
|
|
StoredJobSerializer $storedJobSerializer, |
48
|
|
|
JobFlightManager $jobFlightManager = null |
49
|
|
|
) { |
50
|
|
|
$this->context = $context; |
51
|
|
|
$this->jobStore = $jobStore; |
52
|
|
|
$this->storedJobSerializer = $storedJobSerializer; |
53
|
|
|
$this->jobFlightManager = $jobFlightManager ?: new NullJobFlightManager; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $topicName |
58
|
|
|
* @param int|null $storedJobId |
59
|
|
|
* @throws FailedToEnqueueStoredJobException |
60
|
|
|
* @throws StoredJobNotFoundException |
61
|
|
|
*/ |
62
|
|
|
public function execute(string $topicName, int $storedJobId = null) |
63
|
|
|
{ |
64
|
|
|
$storedJob = $this->jobStore->storedJobOfId($storedJobId); |
65
|
|
|
if (is_null($storedJob)) { |
66
|
|
|
throw new StoredJobNotFoundException; |
67
|
|
|
} |
68
|
|
|
$producer = $this->createProducer(); |
69
|
|
|
$topic = $this->createTopic($topicName); |
70
|
|
|
$message = $this->createMessage($storedJob); |
71
|
|
|
$message->setMessageId($storedJob->id()); |
72
|
|
|
try { |
73
|
|
|
$producer->send($topic, $message); |
74
|
|
|
$this->jobFlightManager->departed( |
75
|
|
|
$message->getMessageId(), |
76
|
|
|
$storedJob->name(), |
77
|
|
|
$topicName |
78
|
|
|
); |
79
|
|
|
} catch (Throwable $e) { |
80
|
|
|
throw new FailedToEnqueueStoredJobException(0, $e); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return PsrProducer |
86
|
|
|
*/ |
87
|
|
|
protected function createProducer(): PsrProducer |
88
|
|
|
{ |
89
|
|
|
$producer = $this->context->createProducer(); |
90
|
|
|
|
91
|
|
|
return $producer; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $topicName |
96
|
|
|
* @return PsrTopic |
97
|
|
|
*/ |
98
|
|
|
protected function createTopic(string $topicName): PsrTopic |
99
|
|
|
{ |
100
|
|
|
$topic = $this->context->createTopic($topicName); |
101
|
|
|
|
102
|
|
|
return $topic; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param StoredJob $storedJob |
107
|
|
|
* @return PsrMessage |
108
|
|
|
*/ |
109
|
|
|
protected function createMessage(StoredJob $storedJob): PsrMessage |
110
|
|
|
{ |
111
|
|
|
$message = $this->context->createMessage($this->storedJobSerializer->serialize($storedJob)); |
112
|
|
|
|
113
|
|
|
return $message; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.