Skip to content

Commit 8c1c0f3

Browse files
committed
WIP: ideas for refactoring repository and subscriptions
1 parent 746203a commit 8c1c0f3

10 files changed

+394
-527
lines changed

src/Contracts/Repository.php

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,29 @@ interface Repository
3434
*/
3535
public function newMessageId(): int;
3636

37-
/**
38-
* Releases the given message id, allowing it to be reused in the future.
39-
*
40-
* @param int $messageId
41-
* @return void
42-
*/
43-
public function releaseMessageId(int $messageId): void;
44-
4537
/**
4638
* Returns the number of registered topic subscriptions. The method does
4739
* not differentiate between pending and acknowledged subscriptions.
4840
*
4941
* @return int
5042
*/
51-
public function countTopicSubscriptions(): int;
43+
public function countSubscriptions(): int;
5244

5345
/**
5446
* Adds a topic subscription to the repository.
5547
*
5648
* @param TopicSubscription $subscription
5749
* @return void
5850
*/
59-
public function addTopicSubscription(TopicSubscription $subscription): void;
51+
public function addSubscription(TopicSubscription $subscription): void;
6052

6153
/**
6254
* Get all topic subscriptions with the given message identifier.
6355
*
6456
* @param int $messageId
6557
* @return TopicSubscription[]
6658
*/
67-
public function getTopicSubscriptionsWithMessageId(int $messageId): array;
59+
public function getSubscriptionsWithMessageId(int $messageId): array;
6860

6961
/**
7062
* Find a topic subscription with the given topic.
@@ -90,7 +82,7 @@ public function getTopicSubscriptionsMatchingTopic(string $topic): array;
9082
* @param string $topic
9183
* @return bool
9284
*/
93-
public function removeTopicSubscription(string $topic): bool;
85+
public function removeSubscription(string $topic): bool;
9486

9587
/**
9688
* Returns the number of pending publish messages.
@@ -115,40 +107,55 @@ public function addPendingPublishedMessage(PublishedMessage $message): void;
115107
*/
116108
public function getPendingPublishedMessageWithMessageId(int $messageId): ?PublishedMessage;
117109

110+
111+
112+
113+
114+
115+
116+
117+
118+
118119
/**
119-
* Gets a list of pending published messages last sent before the given date time.
120+
* Returns the number of pending unsubscribe requests.
120121
*
121-
* @param DateTime $dateTime
122-
* @return PublishedMessage[]
122+
* @return int
123123
*/
124-
public function getPendingPublishedMessagesLastSentBefore(DateTime $dateTime): array;
124+
public function countPendingOutgoingMessages(): int;
125125

126126
/**
127-
* Marks the pending published message with the given message identifier as received.
128-
* If the message has no QoS level of 2, is not found or has already been received,
129-
* false is returned. Otherwise the result will be true.
127+
* Gets a list of pending outgoing messages last sent before the given date time.
130128
*
131-
* @param int $messageId
132-
* @return bool
129+
* @param DateTime $dateTime
130+
* @return PublishedMessage[]
133131
*/
134-
public function markPendingPublishedMessageAsReceived(int $messageId): bool;
132+
public function getPendingOutgoingMessagesLastSentBefore(DateTime $dateTime): array;
133+
135134

136135
/**
137-
* Removes a pending published message from the repository. If a pending message
138-
* with the given identifier is found and successfully removed from the repository,
139-
* `true` is returned. Otherwise `false` will be returned.
136+
* Returns the number of pending publish confirmations.
140137
*
141-
* @param int $messageId
142-
* @return bool
138+
* @return int
143139
*/
144-
public function removePendingPublishedMessage(int $messageId): bool;
140+
public function countPendingPublishConfirmations(): int;
141+
142+
143+
144+
145+
146+
147+
148+
145149

146150
/**
147-
* Returns the number of pending unsubscribe requests.
151+
* Marks the pending published message with the given message identifier as received.
152+
* If the message has no QoS level of 2, is not found or has already been received,
153+
* false is returned. Otherwise the result will be true.
148154
*
149-
* @return int
155+
* @param int $messageId
156+
* @return bool
150157
*/
151-
public function countPendingUnsubscribeRequests(): int;
158+
public function markPendingPublishedMessageAsReceived(int $messageId): bool;
152159

153160
/**
154161
* Adds a pending unsubscribe request to the repository.
@@ -166,13 +173,28 @@ public function addPendingUnsubscribeRequest(UnsubscribeRequest $request): void;
166173
*/
167174
public function getPendingUnsubscribeRequestWithMessageId(int $messageId): ?UnsubscribeRequest;
168175

176+
169177
/**
170-
* Gets a list of pending unsubscribe requests last sent before the given date time.
178+
* Adds a pending publish confirmation to the repository.
171179
*
172-
* @param DateTime $dateTime
173-
* @return UnsubscribeRequest[]
180+
* @param PublishedMessage $message
181+
* @return void
182+
* @throws PendingPublishConfirmationAlreadyExistsException
183+
*/
184+
public function addPendingPublishConfirmation(PublishedMessage $message): void;
185+
186+
/**
187+
* Gets a pending publish confirmation with the given message identifier, if found.
188+
*
189+
* @param int $messageId
190+
* @return PublishedMessage|null
174191
*/
175-
public function getPendingUnsubscribeRequestsLastSentBefore(DateTime $dateTime): array;
192+
public function getPendingPublishConfirmationWithMessageId(int $messageId): ?PublishedMessage;
193+
194+
195+
196+
197+
176198

177199
/**
178200
* Removes a pending unsubscribe requests from the repository. If a pending request
@@ -184,29 +206,16 @@ public function getPendingUnsubscribeRequestsLastSentBefore(DateTime $dateTime):
184206
*/
185207
public function removePendingUnsubscribeRequest(int $messageId): bool;
186208

187-
/**
188-
* Returns the number of pending publish confirmations.
189-
*
190-
* @return int
191-
*/
192-
public function countPendingPublishConfirmations(): int;
193-
194-
/**
195-
* Adds a pending publish confirmation to the repository.
196-
*
197-
* @param PublishedMessage $message
198-
* @return void
199-
* @throws PendingPublishConfirmationAlreadyExistsException
200-
*/
201-
public function addPendingPublishConfirmation(PublishedMessage $message): void;
202209

203210
/**
204-
* Gets a pending publish confirmation with the given message identifier, if found.
211+
* Removes a pending published message from the repository. If a pending message
212+
* with the given identifier is found and successfully removed from the repository,
213+
* `true` is returned. Otherwise `false` will be returned.
205214
*
206215
* @param int $messageId
207-
* @return PublishedMessage|null
216+
* @return bool
208217
*/
209-
public function getPendingPublishConfirmationWithMessageId(int $messageId): ?PublishedMessage;
218+
public function removePendingPublishedMessage(int $messageId): bool;
210219

211220
/**
212221
* Removes the pending publish confirmation with the given message identifier
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMqtt\Client\Exceptions;
6+
7+
/**
8+
* Exception to be thrown if a pending message with the same packet identifier is still pending.
9+
*
10+
* @package PhpMqtt\Client\Exceptions
11+
*/
12+
class PendingMessageAlreadyExistsException extends MqttClientException
13+
{
14+
/**
15+
* PendingMessageAlreadyExistsException constructor.
16+
*
17+
* @param int $messageId
18+
*/
19+
public function __construct(int $messageId)
20+
{
21+
parent::__construct(sprintf('A pending message with the message identifier [%s] exists already.', $messageId));
22+
}
23+
}

src/Exceptions/PendingPublishConfirmationAlreadyExistsException.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)