From a70e94074cacbce928dee4e87b6fe74f55df0b3a Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Tue, 14 May 2024 15:00:56 +0200 Subject: [PATCH] Add proper Fire endpoint --- src/PubNub/Endpoints/PubSub/Fire.php | 221 +++++++++++++++++++++++++++ src/PubNub/PubNub.php | 5 +- tests/functional/FireTest.php | 26 ++++ 3 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 src/PubNub/Endpoints/PubSub/Fire.php create mode 100644 tests/functional/FireTest.php diff --git a/src/PubNub/Endpoints/PubSub/Fire.php b/src/PubNub/Endpoints/PubSub/Fire.php new file mode 100644 index 00000000..3d5e3685 --- /dev/null +++ b/src/PubNub/Endpoints/PubSub/Fire.php @@ -0,0 +1,221 @@ +callback = $callback; + + return $this; + } + + /** + * set the fire message + * + * @param mixed $message + * + * @return $this + */ + public function message($message) + { + $this->message = $message; + + return $this; + } + + /** + * Set the channel name to perform the operation on + * + * @param string $channel The channel name to perform the operation on + * + * @return $this + */ + public function channel($channel) + { + $this->channel = $channel; + + return $this; + } + + /** + * Use POST to publish. + * + * @param bool $usePost + * + * @return $this + */ + public function usePost(bool $usePost) + { + $this->usePost = $usePost; + + return $this; + } + + /** + * Meta data object which can be used with the filtering ability + * + * @param array $meta + * + * @return $this + */ + public function meta($meta) + { + $this->meta = $meta; + + return $this; + } + + /** + * @throws PubNubValidationException + */ + protected function validateParams() + { + if (!is_string($this->channel) || strlen($this->channel) === 0) { + throw new PubNubValidationException("Channel Missing"); + } + + if (!is_string($this->callback) || strlen($this->callback) === 0) { + throw new PubNubValidationException("Callback Missing"); + } + + $this->validateSubscribeKey(); + $this->validatePublishKey(); + } + + /** + * @return string + * @throws PubNubBuildRequestException + */ + protected function buildPath() + { + return sprintf( + static::SIGNAL_PATH, + $this->pubnub->getConfiguration()->getPublishKey(), + $this->pubnub->getConfiguration()->getSubscribeKey(), + PubNubUtil::urlEncode($this->channel), + PubNubUtil::urlEncode($this->callback), + PubNubUtil::urlEncode(PubNubUtil::writeValueAsString($this->message)), + ); + } + + + protected function buildData() + { + return []; + } + + protected function customParams() + { + $result = [ + "store" => "0", + "norep" => "1", + ]; + + if ($this->meta) { + $result['meta'] = PubNubUtil::urlEncode(PubNubUtil::writeValueAsString($this->meta)); + } + return $result; + } + + /** + * @return PNPublishResult + */ + public function sync() + { + return parent::sync(); + } + + /** + * @param array $json Decoded json + * @return PNPublishResult + */ + protected function createResponse($json) + { + $timetoken = floatval($json[2]); + + return new PNPublishResult($timetoken); + } + + /** + * @return bool + */ + protected function isAuthRequired() + { + return true; + } + + /** + * @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::PNSignalOperation; + } + + /** + * @return string + */ + protected function getName() + { + return "Fire"; + } +} diff --git a/src/PubNub/PubNub.php b/src/PubNub/PubNub.php index 419fa7fa..2c778bca 100644 --- a/src/PubNub/PubNub.php +++ b/src/PubNub/PubNub.php @@ -37,6 +37,7 @@ use PubNub\Endpoints\Presence\WhereNow; use PubNub\Endpoints\PubSub\Publish; use PubNub\Endpoints\PubSub\Signal; +use PubNub\Endpoints\PubSub\Fire; use PubNub\Endpoints\Push\AddChannelsToPush; use PubNub\Endpoints\Push\ListPushProvisions; use PubNub\Endpoints\Push\RemoveChannelsFromPush; @@ -143,11 +144,11 @@ public function publish() } /** - * @return Publish + * @return Fire */ public function fire() { - return (new Publish($this))->shouldStore(false)->replicate(false); + return new Fire($this); } /** diff --git a/tests/functional/FireTest.php b/tests/functional/FireTest.php new file mode 100644 index 00000000..0eb62c4d --- /dev/null +++ b/tests/functional/FireTest.php @@ -0,0 +1,26 @@ +pubnub = new PubNub($this->config); + } + + public function testFireSingleChannel() + { + $fireResponse = $this->pubnub->fire()->channel('test')->message('hello')->meta(['env' => 'testing'])->sync(); + + $this->assertTrue($fireResponse instanceof PNPublishResult); + } +}