-
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.
- Loading branch information
Showing
3 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
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,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"; | ||
} | ||
} |
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
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,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); | ||
} | ||
} |