Skip to content

Commit

Permalink
Refactor FcmMessage to make simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson committed Oct 13, 2023
1 parent b203c83 commit 83438d8
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 1,990 deletions.
13 changes: 0 additions & 13 deletions src/Exceptions/InvalidPropertyException.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/FcmChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function send($notifiable, Notification $notification): void

collect($tokens)
->chunk(self::TOKENS_PER_REQUEST)
->map(fn ($tokens) => ($fcmMessage->getClient() ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()))
->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all()))
->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report));
}

Expand Down
275 changes: 42 additions & 233 deletions src/FcmMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,294 +5,118 @@
use Illuminate\Support\Traits\Macroable;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Messaging\Message;
use NotificationChannels\Fcm\Exceptions\InvalidPropertyException;
use NotificationChannels\Fcm\Resources\AndroidConfig;
use NotificationChannels\Fcm\Resources\ApnsConfig;
use NotificationChannels\Fcm\Resources\FcmOptions;
use NotificationChannels\Fcm\Resources\Notification;
use NotificationChannels\Fcm\Resources\WebpushConfig;

class FcmMessage implements Message
{
use Macroable;

/**
* @var string|null
* The message name.
*/
protected $name;
public ?string $name = null;

/**
* @var array|null
* The message token.
*/
protected $data;
public ?string $token = null;

/**
* @var Notification|null
* The message topic.
*/
protected $notification;
public ?string $topic = null;

/**
* @var AndroidConfig|null
* The message condition.
*/
protected $android;
public ?string $condition = null;

/**
* @var WebpushConfig|null
* The message data.
*/
protected $webpush;
public ?array $data = [];

/**
* @var ApnsConfig|null
* The custom message data.
*/
protected $apns;
public array $custom = [];

/**
* @var FcmOptions|null
*/
protected $fcmOptions;

/**
* @var string|null
*/
protected $token;

/**
* @var string|null
*/
protected $topic;

/**
* @var string|null
* The custom messaging client.
*/
protected $condition;
public ?Messaging $client;

/**
* The custom messaging client.
*
* @var \Kreait\Firebase\Contract\Messaging
* Create a new message instance.
*/
protected $client;

public static function create(): self
{
return new self;
}

/**
* @return string|null
* Set the message name.
*/
public function getName(): ?string
{
return $this->name;
}

/**
* @param string|null $name
* @return $this
*/
public function setName(?string $name): self
public function name(?string $name): self
{
$this->name = $name;

return $this;
}

/**
* @return array|null
*/
public function getData(): ?array
{
return $this->data;
}

/**
* @param array<string, string>|null $data
* @return $this
*
* @throws \NotificationChannels\Fcm\Exceptions\InvalidPropertyException
*/
public function setData(?array $data): self
{
foreach ($data as $key => $item) {
if (! is_string($item)) {
throw InvalidPropertyException::mustBeString($key);
}
}

$this->data = $data;

return $this;
}

/**
* @return Notification|null
*/
public function getNotification(): ?Notification
{
return $this->notification;
}

/**
* @param Notification|null $notification
* @return $this
*/
public function setNotification(?Notification $notification): self
{
$this->notification = $notification;

return $this;
}

/**
* @return AndroidConfig|null
*/
public function getAndroid(): ?AndroidConfig
{
return $this->android;
}

/**
* @param AndroidConfig|null $android
* @return $this
*/
public function setAndroid(?AndroidConfig $android): self
{
$this->android = $android;

return $this;
}

/**
* @return WebpushConfig|null
*/
public function getWebpush(): ?WebpushConfig
{
return $this->webpush;
}

/**
* @param WebpushConfig|null $webpush
* @return $this
*/
public function setWebpush(?WebpushConfig $webpush): self
{
$this->webpush = $webpush;

return $this;
}

/**
* @return ApnsConfig|null
* Set the message token.
*/
public function getApns(): ?ApnsConfig
public function token(?string $token): self
{
return $this->apns;
}

/**
* @param ApnsConfig|null $apns
* @return $this
*/
public function setApns(?ApnsConfig $apns): self
{
$this->apns = $apns;
$this->token = $token;

return $this;
}

/**
* @return FcmOptions|null
* Set the message topic.s
*/
public function getFcmOptions(): ?FcmOptions
public function topic(?string $topic): self
{
return $this->fcmOptions;
}

/**
* @param FcmOptions|null $fcmOptions
* @return $this
*/
public function setFcmOptions(?FcmOptions $fcmOptions): self
{
$this->fcmOptions = $fcmOptions;
$this->topic = $topic;

return $this;
}

/**
* @return string|null
*/
public function getToken(): ?string
{
return $this->token;
}

/**
* @param string|null $token
* @return $this
* Set the message condition.
*/
public function setToken(?string $token): self
public function condition(?string $condition): self
{
$this->token = $token;
$this->condition = $condition;

return $this;
}

/**
* @return string|null
*/
public function getTopic(): ?string
{
return $this->topic;
}

/**
* @param string|null $topic
* @return $this
* Set the message data.
*/
public function setTopic(?string $topic): self
public function data(?array $data): self
{
$this->topic = $topic;
$this->data = $data;

return $this;
}

/**
* @return string|null
* Set additional custom message data.
*/
public function getCondition(): ?string
public function custom(?array $custom): self
{
return $this->condition;
}

/**
* @param string|null $condition
* @return $this
*/
public function setCondition(?string $condition): self
{
$this->condition = $condition;
$this->custom = $custom;

return $this;
}

/**
* Get the custom Firebase Messaging client.
*
* @return \Kreait\Firebase\Contract\Messaging|null
* Set the message Firebase Messaging client instance.
*/
public function getClient(): ?Messaging
{
return $this->client;
}

/**
* Set the custom Firebase Messaging client instance.
*
* @param \Kreait\Firebase\Contract\Messaging $client
* @return $this
*/
public function usingClient(Messaging $client)
public function usingClient(Messaging $client): self
{
$this->client = $client;

Expand All @@ -301,29 +125,14 @@ public function usingClient(Messaging $client)

public function toArray()
{
$data = [
'name' => $this->getName(),
'data' => $this->getData(),
'notification' => ! is_null($this->getNotification()) ? $this->getNotification()->toArray() : null,
'android' => ! is_null($this->getAndroid()) ? $this->getAndroid()->toArray() : null,
'webpush' => ! is_null($this->getWebpush()) ? $this->getWebpush()->toArray() : null,
'apns' => ! is_null($this->getApns()) ? $this->getApns()->toArray() : null,
'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null,
];

if ($token = $this->getToken()) {
$data['token'] = $token;
}

if ($topic = $this->getTopic()) {
$data['topic'] = $topic;
}

if ($condition = $this->getCondition()) {
$data['condition'] = $condition;
}

return $data;
return array_filter([
'name' => $this->name,
'data' => $this->data,
'token' => $this->token,
'topic' => $this->topic,
'condition' => $this->condition,
...$this->custom,
]);
}

/**
Expand Down
Loading

0 comments on commit 83438d8

Please sign in to comment.