33namespace Enqueue \Mongodb ;
44
55use Interop \Queue \InvalidDestinationException ;
6+ use Interop \Queue \PsrConsumer ;
67use Interop \Queue \PsrContext ;
78use Interop \Queue \PsrDestination ;
9+ use Interop \Queue \PsrMessage ;
10+ use Interop \Queue \PsrProducer ;
11+ use Interop \Queue \PsrQueue ;
12+ use Interop \Queue \PsrSubscriptionConsumer ;
13+ use Interop \Queue \PsrTopic ;
14+ use Interop \Queue \SubscriptionConsumerNotSupportedException ;
15+ use Interop \Queue \TemporaryQueueNotSupportedException ;
816use MongoDB \Client ;
17+ use MongoDB \Collection ;
918
1019class MongodbContext implements PsrContext
1120{
@@ -30,7 +39,10 @@ public function __construct($client, array $config = [])
3039 $ this ->client = $ client ;
3140 }
3241
33- public function createMessage ($ body = '' , array $ properties = [], array $ headers = [])
42+ /**
43+ * @return MongodbMessage
44+ */
45+ public function createMessage (string $ body = '' , array $ properties = [], array $ headers = []): PsrMessage
3446 {
3547 $ message = new MongodbMessage ();
3648 $ message ->setBody ($ body );
@@ -40,27 +52,41 @@ public function createMessage($body = '', array $properties = [], array $headers
4052 return $ message ;
4153 }
4254
43- public function createTopic ($ name )
55+ /**
56+ * @return MongodbDestination
57+ */
58+ public function createTopic (string $ name ): PsrTopic
4459 {
4560 return new MongodbDestination ($ name );
4661 }
4762
48- public function createQueue ($ queueName )
63+ /**
64+ * @return MongodbDestination
65+ */
66+ public function createQueue (string $ queueName ): PsrQueue
4967 {
5068 return new MongodbDestination ($ queueName );
5169 }
5270
53- public function createTemporaryQueue ()
71+ public function createTemporaryQueue (): PsrQueue
5472 {
55- throw new \ BadMethodCallException ( ' Mongodb transport does not support temporary queues ' );
73+ throw TemporaryQueueNotSupportedException:: providerDoestNotSupportIt ( );
5674 }
5775
58- public function createProducer ()
76+ /**
77+ * @return MongodbProducer
78+ */
79+ public function createProducer (): PsrProducer
5980 {
6081 return new MongodbProducer ($ this );
6182 }
6283
63- public function createConsumer (PsrDestination $ destination )
84+ /**
85+ * @param MongodbDestination $destination
86+ *
87+ * @return MongodbConsumer
88+ */
89+ public function createConsumer (PsrDestination $ destination ): PsrConsumer
6490 {
6591 InvalidDestinationException::assertDestinationInstanceOf ($ destination , MongodbDestination::class);
6692
@@ -73,35 +99,43 @@ public function createConsumer(PsrDestination $destination)
7399 return $ consumer ;
74100 }
75101
76- public function close ()
102+ public function close (): void
103+ {
104+ }
105+
106+ public function createSubscriptionConsumer (): PsrSubscriptionConsumer
77107 {
78- // TODO: Implement close() method.
108+ throw SubscriptionConsumerNotSupportedException:: providerDoestNotSupportIt ();
79109 }
80110
81- public function getCollection ()
111+ /**
112+ * @param MongodbDestination $queue
113+ */
114+ public function purgeQueue (PsrQueue $ queue ): void
115+ {
116+ $ this ->getCollection ()->deleteMany ([
117+ 'queue ' => $ queue ->getQueueName (),
118+ ]);
119+ }
120+
121+ public function getCollection (): Collection
82122 {
83123 return $ this ->client
84124 ->selectDatabase ($ this ->config ['dbname ' ])
85125 ->selectCollection ($ this ->config ['collection_name ' ]);
86126 }
87127
88- /**
89- * @return Client
90- */
91- public function getClient ()
128+ public function getClient (): Client
92129 {
93130 return $ this ->client ;
94131 }
95132
96- /**
97- * @return array
98- */
99- public function getConfig ()
133+ public function getConfig (): array
100134 {
101135 return $ this ->config ;
102136 }
103137
104- public function createCollection ()
138+ public function createCollection (): void
105139 {
106140 $ collection = $ this ->getCollection ();
107141 $ collection ->createIndex (['priority ' => -1 , 'published_at ' => 1 ], ['name ' => 'enqueue_priority ' ]);
0 commit comments