Skip to content

Commit

Permalink
Add proper Fire endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed May 14, 2024
1 parent c2a2446 commit a70e940
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 2 deletions.
221 changes: 221 additions & 0 deletions src/PubNub/Endpoints/PubSub/Fire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php

namespace PubNub\Endpoints\PubSub;

use PubNub\Endpoints\Endpoint;
use PubNub\Enums\PNHttpMethod;
use PubNub\Enums\PNOperationType;
use PubNub\Exceptions\PubNubBuildRequestException;
use PubNub\Exceptions\PubNubValidationException;
use PubNub\Models\Consumer\PNPublishResult;
use PubNub\PubNubUtil;

class Fire extends Endpoint
{
protected const SIGNAL_PATH = "/publish/%s/%s/0/%s/%s/%s";

/** @var string $callback The JSONP callback name to wrap the function in. Use "0" for no callback */
protected string $callback = "0";

/** @var string $channel The channel name to perform the operation on */
protected string $channel;

/** @var mixed $message to publish */
protected mixed $message;

/** @var bool $usePost HTTP method instead of default GET */
protected bool $usePost = false;

/** @var array $meta Meta data object which can be used with the filtering ability */
protected $meta;


/**
* Set the JSONP callback name to wrap the function in. Use "0" for no callback
*
* @param string $callback The JSONP callback name to wrap the function in. Use 0 for no callback
*
* @return $this
*/
public function callback($callback): self
{
$this->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";
}
}
5 changes: 3 additions & 2 deletions src/PubNub/PubNub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/FireTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Functional;

use PubNub\Endpoints\Presence\GetState;
use PubNub\PubNub;
use PubNub\PubNubUtil;
use PubNub\Models\Consumer\PNPublishResult;
use PubNubTestCase;

class FireTest extends PubNubTestCase
{
public function setUp(): void
{
parent::setUp();

$this->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);
}
}

0 comments on commit a70e940

Please sign in to comment.