Skip to content

Commit 3b99105

Browse files
committed
[psr] Introduce MessageProcessor interface (moved from consumption).
It would allow to decouple consumption code from consumption component. It must depends on psr queue only.
1 parent 192637a commit 3b99105

File tree

4 files changed

+82
-57
lines changed

4 files changed

+82
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
<?php
22
namespace Enqueue\Consumption;
33

4-
use Enqueue\Psr\Context as PsrContext;
5-
use Enqueue\Psr\Message as PsrMessage;
4+
use Enqueue\Psr\MessageProcessor;
65

7-
interface MessageProcessorInterface
6+
/**
7+
* @deprecated
8+
*/
9+
interface MessageProcessorInterface extends MessageProcessor
810
{
9-
/**
10-
* @param PsrMessage $message
11-
* @param PsrContext $context
12-
*
13-
* @return string
14-
*/
15-
public function process(PsrMessage $message, PsrContext $context);
1611
}

pkg/enqueue/Consumption/Result.php

+11-47
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,33 @@
11
<?php
22
namespace Enqueue\Consumption;
33

4-
use Enqueue\Psr\Message;
4+
use Enqueue\Psr\Message as PsrMessage;
5+
use Enqueue\Psr\Result as PsrResult;
56

6-
class Result
7+
class Result extends PsrResult
78
{
8-
/**
9-
* Use this constant when the message is processed successfully and the message could be removed from the queue.
10-
*/
11-
const ACK = 'enqueue.message_queue.consumption.ack';
12-
13-
/**
14-
* Use this constant when the message is not valid or could not be processed
15-
* The message is removed from the queue.
16-
*/
17-
const REJECT = 'enqueue.message_queue.consumption.reject';
18-
19-
/**
20-
* Use this constant when the message is not valid or could not be processed right now but we can try again later
21-
* The original message is removed from the queue but a copy is publsihed to the queue again.
22-
*/
23-
const REQUEUE = 'enqueue.message_queue.consumption.requeue';
24-
25-
/**
26-
* @var string
27-
*/
28-
private $status;
29-
309
/**
3110
* @var string
3211
*/
3312
private $reason;
3413

3514
/**
36-
* @var Message|null
15+
* @var PsrMessage|null
3716
*/
3817
private $reply;
3918

4019
/**
41-
* @return Message|null
20+
* @return PsrMessage|null
4221
*/
4322
public function getReply()
4423
{
4524
return $this->reply;
4625
}
4726

4827
/**
49-
* @param Message|null $reply
28+
* @param PsrMessage|null $reply
5029
*/
51-
public function setReply(Message $reply = null)
30+
public function setReply(PsrMessage $reply = null)
5231
{
5332
$this->reply = $reply;
5433
}
@@ -59,16 +38,9 @@ public function setReply(Message $reply = null)
5938
*/
6039
public function __construct($status, $reason = '')
6140
{
62-
$this->status = (string) $status;
63-
$this->reason = (string) $reason;
64-
}
41+
parent::__construct($status);
6542

66-
/**
67-
* @return string
68-
*/
69-
public function getStatus()
70-
{
71-
return $this->status;
43+
$this->reason = (string) $reason;
7244
}
7345

7446
/**
@@ -110,24 +82,16 @@ public static function requeue($reason = '')
11082
}
11183

11284
/**
113-
* @param Message $replyMessage
85+
* @param PsrMessage $replyMessage
11486
* @param string|null $reason
11587
*
11688
* @return Result
11789
*/
118-
public static function reply(Message $replyMessage, $reason = '')
90+
public static function reply(PsrMessage $replyMessage, $reason = '')
11991
{
12092
$result = static::ack($reason);
12193
$result->setReply($replyMessage);
12294

12395
return $result;
12496
}
125-
126-
/**
127-
* @return string
128-
*/
129-
public function __toString()
130-
{
131-
return $this->status;
132-
}
13397
}

pkg/psr-queue/MessageProcessor.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Enqueue\Psr;
3+
4+
interface MessageProcessor
5+
{
6+
/**
7+
* The method has to return eitherResult::ACK, Result::REJECT, Result::REQUEUE string or instance of Result.
8+
*
9+
* @param Message $message
10+
* @param Context $context
11+
*
12+
* @return string|Result
13+
*/
14+
public function process(Message $message, Context $context);
15+
}

pkg/psr-queue/Result.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace Enqueue\Psr;
3+
4+
class Result
5+
{
6+
/**
7+
* Use this constant when the message is processed successfully and the message could be removed from the queue.
8+
*/
9+
const ACK = 'enqueue.message_queue.consumption.ack';
10+
11+
/**
12+
* Use this constant when the message is not valid or could not be processed
13+
* The message is removed from the queue.
14+
*/
15+
const REJECT = 'enqueue.message_queue.consumption.reject';
16+
17+
/**
18+
* Use this constant when the message is not valid or could not be processed right now but we can try again later
19+
* The original message is removed from the queue but a copy is publsihed to the queue again.
20+
*/
21+
const REQUEUE = 'enqueue.message_queue.consumption.requeue';
22+
23+
/**
24+
* @var string
25+
*/
26+
private $status;
27+
28+
/**
29+
* @param string $status
30+
*/
31+
public function __construct($status)
32+
{
33+
$this->status = (string) $status;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getStatus()
40+
{
41+
return $this->status;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function __toString()
48+
{
49+
return $this->status;
50+
}
51+
}

0 commit comments

Comments
 (0)