-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/fire-endpoint
- Loading branch information
Showing
6 changed files
with
577 additions
and
4 deletions.
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
src/PubNub/Endpoints/MessagePersistance/FetchMessages.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
<?php | ||
|
||
namespace PubNub\Endpoints\MessagePersistance; | ||
|
||
use PubNub\Endpoints\Endpoint; | ||
use PubNub\Enums\PNOperationType; | ||
use PubNub\Enums\PNHttpMethod; | ||
use PubNub\Exceptions\PubNubBuildRequestException; | ||
use PubNub\Exceptions\PubNubValidationException; | ||
use PubNub\Models\Consumer\MessagePersistence\PNFetchMessagesResult; | ||
use PubNub\PubNubUtil; | ||
|
||
class FetchMessages extends Endpoint | ||
{ | ||
protected const GET_PATH = "/v3/history%s/sub-key/%s/channel/%s"; | ||
|
||
protected const SINGLE_CHANNEL_MAX_MESSAGES = 100; | ||
protected const DEFAULT_SINGLE_CHANNEL_MESSAGES = 100; | ||
|
||
protected const MULTIPLE_CHANNELS_MAX_MESSAGES = 25; | ||
protected const DEFAULT_MULTIPLE_CHANNELS_MESSAGES = 25; | ||
|
||
protected const MAX_MESSAGES_ACTIONS = 25; | ||
protected const DEFAULT_MESSAGES_ACTIONS = 25; | ||
|
||
protected array $channels; | ||
|
||
protected int $start; | ||
protected int $end; | ||
protected int $count; | ||
|
||
protected bool $includeMeta = false; | ||
protected bool $includeUuid = false; | ||
protected bool $includeMessageType = false; | ||
protected bool $includeMessageActions = false; | ||
|
||
protected array $customParamMapping = [ | ||
'start' => 'start', | ||
'end' => 'end', | ||
'count' => 'max', | ||
'includeMeta' => 'include_meta', | ||
'includeUuid' => 'include_uuid', | ||
'includeMessageType' => 'include_message_type', | ||
]; | ||
|
||
public function channels(...$channel) | ||
{ | ||
if (is_array($channel[0])) { | ||
$this->channels = $channel[0]; | ||
} elseif (strpos($channel[0], ',')) { | ||
$this->channels = array_map('trim', explode(',', $channel[0])); | ||
} else { | ||
$this->channels = $channel; | ||
} | ||
return $this; | ||
} | ||
|
||
public function start($start) | ||
{ | ||
$this->start = $start; | ||
return $this; | ||
} | ||
|
||
public function end($end) | ||
{ | ||
$this->end = $end; | ||
return $this; | ||
} | ||
|
||
public function count($count) | ||
{ | ||
$this->count = $count; | ||
return $this; | ||
} | ||
|
||
public function includeMeta($includeMeta) | ||
{ | ||
$this->includeMeta = $includeMeta; | ||
return $this; | ||
} | ||
|
||
public function includeUuid($includeUuid) | ||
{ | ||
$this->includeUuid = $includeUuid; | ||
return $this; | ||
} | ||
|
||
public function includeMessageType($includeMessageType) | ||
{ | ||
$this->includeMessageType = $includeMessageType; | ||
return $this; | ||
} | ||
|
||
public function includeMessageActions($includeMessageActions) | ||
{ | ||
$this->includeMessageActions = $includeMessageActions; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @throws PubNubValidationException | ||
*/ | ||
protected function validateParams() | ||
{ | ||
if (!is_array($this->channels) || count($this->channels) === 0) { | ||
throw new PubNubValidationException("Channel Missing"); | ||
} | ||
|
||
$this->validateSubscribeKey(); | ||
$this->validatePublishKey(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function customParams() | ||
{ | ||
$params = []; | ||
foreach ($this->customParamMapping as $customParam => $requestParam) { | ||
if (isset($this->$customParam) && !empty($this->$customParam)) { | ||
$params[$requestParam] = $this->$customParam; | ||
} | ||
} | ||
|
||
return $params; | ||
} | ||
|
||
/** | ||
* @return string | ||
* @throws PubNubBuildRequestException | ||
*/ | ||
protected function buildPath() | ||
{ | ||
$withActions = $this->includeMessageActions ? '-with-actions' : ''; | ||
$channelList = $this->includeMessageActions | ||
? PubNubUtil::urlEncode($this->channels[0]) | ||
: implode(',', array_map(fn($channel) => PubNubUtil::urlEncode($channel), $this->channels)); | ||
|
||
return sprintf( | ||
self::GET_PATH, | ||
$withActions, | ||
$this->pubnub->getConfiguration()->getSubscribeKey(), | ||
$channelList, | ||
); | ||
} | ||
|
||
public function sync(): PNFetchMessagesResult | ||
{ | ||
return parent::sync(); | ||
} | ||
|
||
/** | ||
* @param array $json Decoded json | ||
* @return PNPublishResult | ||
*/ | ||
protected function createResponse($json) | ||
{ | ||
return PNFetchMessagesResult::fromJson( | ||
$json, | ||
$this->pubnub->getCrypto(), | ||
isset($this->start) ? $this->start : null, | ||
isset($this->end) ? $this->end : null | ||
); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
protected function isAuthRequired() | ||
{ | ||
return true; | ||
} | ||
|
||
protected function buildData() | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
protected function getRequestTimeout() | ||
{ | ||
return $this->pubnub->getConfiguration()->getNonSubscribeRequestTimeout(); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
protected function getConnectTimeout() | ||
{ | ||
return $this->pubnub->getConfiguration()->getConnectTimeout(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function httpMethod() | ||
{ | ||
return PNHttpMethod::GET; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
protected function getOperationType() | ||
{ | ||
return PNOperationType::PNFetchMessagesOperation; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getName() | ||
{ | ||
return "Fetch Messages"; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/PubNub/Models/Consumer/MessagePersistence/PNFetchMessagesItemResult.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace PubNub\Models\Consumer\MessagePersistence; | ||
|
||
class PNFetchMessagesItemResult | ||
{ | ||
protected mixed $message; | ||
protected string $timetoken; | ||
protected mixed $metadata; | ||
protected mixed $actions; | ||
protected string $uuid; | ||
protected string $messageType; | ||
|
||
|
||
public function __construct(mixed $message, string $timetoken) | ||
{ | ||
$this->message = $message; | ||
$this->timetoken = $timetoken; | ||
} | ||
|
||
public function setMetadata(mixed $metadata) | ||
{ | ||
$this->metadata = $metadata; | ||
return $this; | ||
} | ||
|
||
public function setActions(mixed $actions) | ||
{ | ||
$this->actions = $actions; | ||
return $this; | ||
} | ||
|
||
public function setUuid(string $uuid) | ||
{ | ||
$this->uuid = $uuid; | ||
return $this; | ||
} | ||
|
||
public function setMessageType(string $messageType) | ||
{ | ||
$this->messageType = $messageType; | ||
return $this; | ||
} | ||
|
||
public function getMessage(): mixed | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function getTimetoken(): string | ||
{ | ||
return $this->timetoken; | ||
} | ||
|
||
public function getMetadata(): mixed | ||
{ | ||
return $this->metadata; | ||
} | ||
|
||
public function getActions(): mixed | ||
{ | ||
return $this->actions; | ||
} | ||
|
||
public function getUuid(): string | ||
{ | ||
return $this->uuid; | ||
} | ||
|
||
public function getMessageType(): string | ||
{ | ||
return $this->messageType; | ||
} | ||
|
||
public static function fromJson($json, $crypto): static | ||
{ | ||
$message = $json['message']; | ||
if ($crypto) { | ||
$message = $crypto->decrypt($message); | ||
} | ||
$item = new static( | ||
$message, | ||
$json['timetoken'], | ||
); | ||
|
||
if (isset($json['uuid'])) { | ||
$item->setUuid($json['uuid']); | ||
} | ||
|
||
if (isset($json['message_type'])) { | ||
$item->setMessageType($json['message_type']); | ||
} | ||
|
||
if (isset($json['meta'])) { | ||
$item->setMetadata($json['meta']); | ||
} | ||
|
||
if (isset($json['actions'])) { | ||
$item->setActions($json['actions']); | ||
} else { | ||
$item->setActions([]); | ||
} | ||
|
||
return $item; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf("Fetch message item with tt: %s and content: %s", $this->timetoken, $this->message); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/PubNub/Models/Consumer/MessagePersistence/PNFetchMessagesResult.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace PubNub\Models\Consumer\MessagePersistence; | ||
|
||
use PubNub\Models\Consumer\MessagePersistence\PNFetchMessagesItemResult; | ||
|
||
class PNFetchMessagesResult | ||
{ | ||
private array $channels; | ||
|
||
/** @var int */ | ||
private $startTimetoken; | ||
|
||
/** @var int */ | ||
private $endTimetoken; | ||
|
||
|
||
public function __construct($channels, $startTimetoken, $endTimetoken) | ||
{ | ||
$this->channels = $channels; | ||
$this->startTimetoken = $startTimetoken; | ||
$this->endTimetoken = $endTimetoken; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return sprintf("Fetch messages result for range %d..%d", $this->startTimetoken, $this->endTimetoken); | ||
} | ||
|
||
public static function fromJson($jsonInput, $crypto, $startTimetoken, $endTimetoken) | ||
{ | ||
$channels = []; | ||
|
||
foreach ($jsonInput['channels'] as $channel => $messages) { | ||
foreach ($messages as $item) { | ||
$channels[$channel][] = PNFetchMessagesItemResult::fromJson($item, $crypto); | ||
} | ||
} | ||
return new static($channels, $startTimetoken, $endTimetoken); | ||
} | ||
|
||
public function getChannels() | ||
{ | ||
return $this->channels; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getStartTimetoken() | ||
{ | ||
return $this->startTimetoken; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getEndTimetoken() | ||
{ | ||
return $this->endTimetoken; | ||
} | ||
} |
Oops, something went wrong.