Skip to content

Commit 4519083

Browse files
committed
WIP: ideas for refactoring repository and subscriptions
1 parent c169c24 commit 4519083

File tree

6 files changed

+205
-370
lines changed

6 files changed

+205
-370
lines changed

src/PendingMessage.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMqtt\Client;
6+
7+
use DateTime;
8+
9+
/**
10+
* Represents a pending message.
11+
*
12+
* If the message is not acknowledged by the broker, having one of these
13+
* objects allows the client to resend the request.
14+
*
15+
* @package PhpMqtt\Client
16+
*/
17+
abstract class PendingMessage
18+
{
19+
/** @var int */
20+
protected $messageId;
21+
22+
/** @var int */
23+
protected $sendingAttempts = 1;
24+
25+
/** @var DateTime */
26+
protected $lastSentAt;
27+
28+
/**
29+
* Creates a new pending message object.
30+
*
31+
* @param int $messageId
32+
* @param DateTime|null $sentAt
33+
*/
34+
protected function __construct(int $messageId, DateTime $sentAt = null)
35+
{
36+
$this->messageId = $messageId;
37+
$this->topic = $topic;
38+
$this->lastSentAt = $sentAt ?? new DateTime();
39+
}
40+
41+
/**
42+
* Returns the message identifier.
43+
*
44+
* @return int
45+
*/
46+
public function getMessageId(): int
47+
{
48+
return $this->messageId;
49+
}
50+
51+
/**
52+
* Returns the date time when the message was last attempted to be sent.
53+
*
54+
* @return DateTime
55+
*/
56+
public function getLastSentAt(): DateTime
57+
{
58+
return $this->lastSentAt;
59+
}
60+
61+
/**
62+
* Returns the number of times the message has been attempted to be sent.
63+
*
64+
* @return int
65+
*/
66+
public function getSendingAttempts(): int
67+
{
68+
return $this->sendingAttempts;
69+
}
70+
71+
/**
72+
* Sets the date time when the message was last attempted to be sent.
73+
*
74+
* @param DateTime|null $value
75+
* @return static
76+
*/
77+
public function setLastSentAt(DateTime $value = null): self
78+
{
79+
$this->lastSentAt = $value ?? new DateTime();
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* Increments the sending attempts by one.
86+
*
87+
* @return static
88+
*/
89+
public function incrementSendingAttempts(): self
90+
{
91+
$this->sendingAttempts++;
92+
93+
return $this;
94+
}
95+
}

src/PublishedMessage.php

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
*
1313
* @package PhpMqtt\Client
1414
*/
15-
class PublishedMessage
15+
class PublishedMessage extends PendingMessage
1616
{
17-
/** @var int */
18-
private $messageId;
19-
2017
/** @var string */
2118
private $topic;
2219

@@ -29,12 +26,6 @@ class PublishedMessage
2926
/** @var bool */
3027
private $retain;
3128

32-
/** @var DateTime */
33-
private $lastSentAt;
34-
35-
/** @var int */
36-
private $sendingAttempts = 1;
37-
3829
/** @var bool */
3930
private $received = false;
4031

@@ -61,22 +52,10 @@ public function __construct(
6152
$sentAt = new DateTime();
6253
}
6354

64-
$this->messageId = $messageId;
6555
$this->topic = $topic;
6656
$this->message = $message;
6757
$this->qualityOfService = $qualityOfService;
6858
$this->retain = $retain;
69-
$this->lastSentAt = $sentAt;
70-
}
71-
72-
/**
73-
* Returns the message identifier.
74-
*
75-
* @return int
76-
*/
77-
public function getMessageId(): int
78-
{
79-
return $this->messageId;
8059
}
8160

8261
/**
@@ -119,26 +98,6 @@ public function wantsToBeRetained(): bool
11998
return $this->retain;
12099
}
121100

122-
/**
123-
* Returns the date time when the message was last attempted to be sent.
124-
*
125-
* @return DateTime
126-
*/
127-
public function getLastSentAt(): DateTime
128-
{
129-
return $this->lastSentAt;
130-
}
131-
132-
/**
133-
* Returns the number of times the message has been attempted to be sent.
134-
*
135-
* @return int
136-
*/
137-
public function getSendingAttempts(): int
138-
{
139-
return $this->sendingAttempts;
140-
}
141-
142101
/**
143102
* Determines whether the message has been confirmed as received.
144103
*
@@ -149,31 +108,6 @@ public function hasBeenReceived(): bool
149108
return $this->received;
150109
}
151110

152-
/**
153-
* Sets the date time when the message was last attempted to be sent.
154-
*
155-
* @param DateTime $value
156-
* @return static
157-
*/
158-
public function setLastSentAt(DateTime $value): self
159-
{
160-
$this->lastSentAt = $value;
161-
162-
return $this;
163-
}
164-
165-
/**
166-
* Increments the sending attempts by one.
167-
*
168-
* @return static
169-
*/
170-
public function incrementSendingAttempts(): self
171-
{
172-
$this->sendingAttempts++;
173-
174-
return $this;
175-
}
176-
177111
/**
178112
* Sets the received state.
179113
*

0 commit comments

Comments
 (0)