Skip to content

Commit 77637fa

Browse files
committed
Rename CloudMessage target methods to with* pattern
1 parent 95cd744 commit 77637fa

5 files changed

Lines changed: 78 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ If it saves you or your team time, please consider [sponsoring its development](
2525
* `psr/log` has been moved from runtime dependencies to development dependencies
2626
* `Kreait\Firebase\Contract\Messaging::BATCH_MESSAGE_LIMIT` constant has been removed
2727
* Exception codes are no longer preserved when wrapping exceptions
28+
* `Kreait\Firebase\Messaging\CloudMessage` builder methods have been renamed to follow the `with*` pattern:
29+
`toToken()` -> `withToken()`, `toTopic()` -> `withTopic()`, `toCondition()` -> `withCondition()`.
30+
The old methods are deprecated but still available as aliases.
2831

2932
See **[UPGRADE-8.0](UPGRADE-8.0.md) for more details on the changes between 7.x and 8.0.**
3033

UPGRADE-8.0.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ The `Kreait\Firebase\Contract\Messaging::BATCH_MESSAGE_LIMIT` constant has been
4444
### Exception Handling Changes
4545
Exception codes are no longer preserved when wrapping exceptions. If your code relies on specific exception codes for error handling, you should update it to use exception types or messages instead.
4646

47+
### Cloud Message Builder Method Renames
48+
49+
The `CloudMessage` builder methods for setting message targets have been renamed to follow the `with*` naming pattern for consistency with other builder methods in the SDK:
50+
51+
- `toToken()` -> `withToken()`
52+
- `toTopic()` -> `withTopic()`
53+
- `toCondition()` -> `withCondition()`
54+
55+
**Migration:**
56+
57+
Replace the old method names with the new ones:
58+
59+
```php
60+
// Before (7.x)
61+
$message = CloudMessage::new()
62+
->toToken('device-token')
63+
->withNotification(['title' => 'Hello']);
64+
65+
// After (8.0)
66+
$message = CloudMessage::new()
67+
->withToken('device-token')
68+
->withNotification(['title' => 'Hello']);
69+
```
70+
71+
The old methods are still available as deprecated aliases, so your code will continue to work during the transition period.
72+
4773
---
4874

4975
**See the complete list of breaking changes below** to identify any adjustments needed. Most changes should (hopefully)

docs/cloud-messaging.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Getting started
3333
$message = CloudMessage::new()
3434
->withNotification(Notification::create('Title', 'Body'))
3535
->withData(['key' => 'value'])
36-
->toToken('...')
37-
// ->toTopic('...')
38-
// ->toCondition('...')
36+
->withToken('...')
37+
// ->withTopic('...')
38+
// ->withCondition('...')
3939
;
4040
4141
$messaging->send($message);
@@ -79,7 +79,7 @@ You can create a message to a topic in one of the following ways:
7979
$message = CloudMessage::new()
8080
->withNotification($notification) // optional
8181
->withData($data) // optional
82-
->toTopic($topic)
82+
->withTopic($topic)
8383
;
8484
8585
$message = CloudMessage::fromArray([
@@ -132,7 +132,7 @@ Likewise, a user who does not subscribe to TopicA does not receive the message.
132132
$message = CloudMessage::new()
133133
->withNotification($notification) // optional
134134
->withData($data) // optional
135-
->toCondition($condition)
135+
->withCondition($condition)
136136
;
137137
138138
$message = CloudMessage::fromArray([
@@ -161,7 +161,7 @@ for each end-user client app instance.
161161
$message = CloudMessage::new()
162162
->withNotification($notification) // optional
163163
->withData($data) // optional
164-
->toToken($deviceToken)
164+
->withToken($deviceToken)
165165
;
166166
167167
$message = CloudMessage::fromArray([

src/Messaging/CloudMessage.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public function withHighestPossiblePriority(): self
233233
/**
234234
* @param non-empty-string $token
235235
*/
236-
public function toToken(string $token): self
236+
public function withToken(string $token): self
237237
{
238238
$new = clone $this;
239239
$new->target = MessageTarget::with(MessageTarget::TOKEN, $token);
@@ -244,7 +244,7 @@ public function toToken(string $token): self
244244
/**
245245
* @param non-empty-string $topic
246246
*/
247-
public function toTopic(string $topic): self
247+
public function withTopic(string $topic): self
248248
{
249249
$new = clone $this;
250250
$new->target = MessageTarget::with(MessageTarget::TOPIC, $topic);
@@ -255,14 +255,47 @@ public function toTopic(string $topic): self
255255
/**
256256
* @param non-empty-string $condition
257257
*/
258-
public function toCondition(string $condition): self
258+
public function withCondition(string $condition): self
259259
{
260260
$new = clone $this;
261261
$new->target = MessageTarget::with(MessageTarget::CONDITION, $condition);
262262

263263
return $new;
264264
}
265265

266+
/**
267+
* @param non-empty-string $token
268+
*
269+
* @deprecated 8.0.0 Use withToken() instead
270+
* @codeCoverageIgnore
271+
*/
272+
public function toToken(string $token): self
273+
{
274+
return $this->withToken($token);
275+
}
276+
277+
/**
278+
* @param non-empty-string $topic
279+
*
280+
* @deprecated 8.0.0 Use withTopic() instead
281+
* @codeCoverageIgnore
282+
*/
283+
public function toTopic(string $topic): self
284+
{
285+
return $this->withTopic($topic);
286+
}
287+
288+
/**
289+
* @param non-empty-string $condition
290+
*
291+
* @deprecated 8.0.0 Use withCondition() instead
292+
* @codeCoverageIgnore
293+
*/
294+
public function toCondition(string $condition): self
295+
{
296+
return $this->withCondition($condition);
297+
}
298+
266299
/**
267300
* @return MessageOutputShape
268301
*/

tests/Integration/MessagingTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function testSendingAMessageWithEmptyMessageDataShouldNotFail(): void
140140
{
141141
$message = CloudMessage::new()
142142
->withData([])
143-
->toToken($this->getTestRegistrationToken())
143+
->withToken($this->getTestRegistrationToken())
144144
;
145145

146146
$result = $this->messaging->send($message);
@@ -158,7 +158,7 @@ public function sendMessageWithReservedKeywordInMessageDataThatIsStillAccepted(s
158158
{
159159
$message = CloudMessage::new()
160160
->withData([$keyword => 'value'])
161-
->toToken($this->getTestRegistrationToken())
161+
->withToken($this->getTestRegistrationToken())
162162
;
163163

164164
$result = $this->messaging->send($message);
@@ -271,10 +271,10 @@ public function testSendMessageToDifferentTargets(): void
271271
$message = CloudMessage::new()->withNotification(['title' => 'Token Notification', 'body' => 'Token body']);
272272
$invalidMessage = new RawMessageFromArray(['invalid' => 'message']);
273273

274-
$tokenMessage = $message->toToken($token);
275-
$topicMessage = $message->toTopic($topic);
276-
$conditionMessage = $message->toCondition($condition);
277-
$invalidToken = $message->toToken($invalidToken);
274+
$tokenMessage = $message->withToken($token);
275+
$topicMessage = $message->withTopic($topic);
276+
$conditionMessage = $message->withCondition($condition);
277+
$invalidToken = $message->withToken($invalidToken);
278278

279279
$messages = [$tokenMessage, $topicMessage, $conditionMessage, $invalidToken, $invalidMessage];
280280

@@ -406,7 +406,7 @@ public function testSendWebPushNotificationWithAnEmptyTitle(): void
406406
'title' => '',
407407
],
408408
]))
409-
->toToken($this->getTestRegistrationToken());
409+
->withToken($this->getTestRegistrationToken());
410410

411411
$this->messaging->send($message);
412412
}

0 commit comments

Comments
 (0)