66
77use Enqueue \Sns \SnsContext ;
88use Enqueue \Sns \SnsProducer ;
9+ use Enqueue \Sqs \SqsContext ;
10+ use Enqueue \Sqs \SqsProducer ;
911use Interop \Queue \Destination ;
1012use Interop \Queue \Exception \InvalidDestinationException ;
1113use Interop \Queue \Exception \InvalidMessageException ;
@@ -17,16 +19,27 @@ class SnsQsProducer implements Producer
1719 /**
1820 * @var SnsContext
1921 */
20- private $ context ;
22+ private $ snsContext ;
2123
2224 /**
2325 * @var SnsProducer
2426 */
25- private $ producer ;
27+ private $ snsProducer ;
2628
27- public function __construct (SnsContext $ context )
29+ /**
30+ * @var SqsContext
31+ */
32+ private $ sqsContext ;
33+
34+ /**
35+ * @var SqsProducer
36+ */
37+ private $ sqsProducer ;
38+
39+ public function __construct (SnsContext $ snsContext , SqsContext $ sqsContext )
2840 {
29- $ this ->context = $ context ;
41+ $ this ->snsContext = $ snsContext ;
42+ $ this ->sqsContext = $ sqsContext ;
3043 }
3144
3245 /**
@@ -35,56 +48,89 @@ public function __construct(SnsContext $context)
3548 */
3649 public function send (Destination $ destination , Message $ message ): void
3750 {
38- InvalidDestinationException::assertDestinationInstanceOf ($ destination , SnsQsTopic::class);
3951 InvalidMessageException::assertMessageInstanceOf ($ message , SnsQsMessage::class);
4052
41- $ snsMessage = $ this ->context ->createMessage ($ message ->getBody (), $ message ->getProperties (), $ message ->getHeaders ());
53+ if (false == $ destination instanceof SnsQsTopic && false == $ destination instanceof SnsQsQueue) {
54+ throw new InvalidDestinationException (sprintf (
55+ 'The destination must be an instance of [%s|%s] but got %s. ' ,
56+ SnsQsTopic::class, SnsQsQueue::class,
57+ is_object ($ destination ) ? get_class ($ destination ) : gettype ($ destination )
58+ ));
59+ }
4260
43- $ this ->getProducer ()->send ($ destination , $ snsMessage );
61+ if ($ destination instanceof SnsQsTopic) {
62+ $ snsMessage = $ this ->snsContext ->createMessage (
63+ $ message ->getBody (),
64+ $ message ->getProperties (),
65+ $ message ->getHeaders ()
66+ );
67+
68+ $ this ->getSnsProducer ()->send ($ destination , $ snsMessage );
69+ } else {
70+ $ sqsMessage = $ this ->sqsContext ->createMessage (
71+ $ message ->getBody (),
72+ $ message ->getProperties (),
73+ $ message ->getHeaders ()
74+ );
75+
76+ $ this ->getSqsProducer ()->send ($ destination , $ sqsMessage );
77+ }
4478 }
4579
4680 public function setDeliveryDelay (int $ deliveryDelay = null ): Producer
4781 {
48- $ this ->getProducer ()->setDeliveryDelay ($ deliveryDelay );
82+ $ this ->getSnsProducer ()->setDeliveryDelay ($ deliveryDelay );
83+ $ this ->getSqsProducer ()->setDeliveryDelay ($ deliveryDelay );
4984
5085 return $ this ;
5186 }
5287
5388 public function getDeliveryDelay (): ?int
5489 {
55- return $ this ->getProducer ()->getDeliveryDelay ();
90+ return $ this ->getSnsProducer ()->getDeliveryDelay ();
5691 }
5792
5893 public function setPriority (int $ priority = null ): Producer
5994 {
60- $ this ->getProducer ()->setPriority ($ priority );
95+ $ this ->getSnsProducer ()->setPriority ($ priority );
96+ $ this ->getSqsProducer ()->setPriority ($ priority );
6197
6298 return $ this ;
6399 }
64100
65101 public function getPriority (): ?int
66102 {
67- return $ this ->getProducer ()->getPriority ();
103+ return $ this ->getSnsProducer ()->getPriority ();
68104 }
69105
70106 public function setTimeToLive (int $ timeToLive = null ): Producer
71107 {
72- $ this ->getProducer ()->setTimeToLive ($ timeToLive );
108+ $ this ->getSnsProducer ()->setTimeToLive ($ timeToLive );
109+ $ this ->getSqsProducer ()->setTimeToLive ($ timeToLive );
73110
74111 return $ this ;
75112 }
76113
77114 public function getTimeToLive (): ?int
78115 {
79- return $ this ->getProducer ()->getTimeToLive ();
116+ return $ this ->getSnsProducer ()->getTimeToLive ();
117+ }
118+
119+ private function getSnsProducer (): SnsProducer
120+ {
121+ if (null === $ this ->snsProducer ) {
122+ $ this ->snsProducer = $ this ->snsContext ->createProducer ();
123+ }
124+
125+ return $ this ->snsProducer ;
80126 }
81127
82- private function getProducer (): SnsProducer
128+ private function getSqsProducer (): SqsProducer
83129 {
84- if (null === $ this ->producer ) {
85- $ this ->producer = $ this ->context ->createProducer ();
130+ if (null === $ this ->sqsProducer ) {
131+ $ this ->sqsProducer = $ this ->sqsContext ->createProducer ();
86132 }
87133
88- return $ this ->producer ;
134+ return $ this ->sqsProducer ;
89135 }
90136}
0 commit comments