Skip to content

Commit

Permalink
fixed examples, added cmt to file, tested. it is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Nov 5, 2024
1 parent 0d90f33 commit 6413a4c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 19 deletions.
35 changes: 35 additions & 0 deletions examples/cli/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

set_time_limit(0);

require('../../vendor/autoload.php');


use PubNub\PubNub;
use PubNub\PNConfiguration;

if ($argc < 3) {
echo "Usage: php file.php <uuid> <channel> <message> <custom_message_type>\n";
exit(1);
}

$pnUuid = $argv[1] . '-pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
$pnConfig->setUuid($pnUuid);

$pubnub = new PubNub($pnConfig);
$file = fopen('pn.jpg', 'rb');
$pubResult = $pubnub->sendFile()
->channel($argv[2])
->message($argv[3])
->customMessageType($argv[4])
->fileHandle($file)
->fileName('pn.jpg')
->sync();

print("Published file");
24 changes: 15 additions & 9 deletions examples/cli/hist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@
$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
// $pnConfig->setSecretKey('sec-c-YjZiZWQzZWItMmZlNS00NjBlLTkyNTUtOGFhZjZiY2E1ZDc1');

$pnConfig->setUuid($pnUuid);
if (array_key_exists(2, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[2], true));
}

$pubnub = new PubNub($pnConfig);

$ts = $pubnub->time()->sync();

$channelName = $argv[1];
$history = $pubnub->history()
->channel($channelName)
->reverse(false)
$history = $pubnub->fetchMessages()
->channels($channelName)
->end((int)($ts->getTimetoken() - 3000000000))
->includeCustomMessageType(true)
->count(50)
->sync();

foreach ($history->getMessages() as $key => $message) {
print($message);
$messages = $history->getChannels()[$channelName];
print("Received " . count($messages) . " messages\n");

foreach ($messages as $key => $message) {
printf("Message %d:\n", $key + 1);
printf(
"\n %s\n Timetoken: %s\n Custom message type: %s\n",
$message->getMessage(),
$message->getTimetoken(),
$message->getCustomMessageType(),
);
}
17 changes: 12 additions & 5 deletions examples/cli/pub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@

use PubNub\PubNub;
use PubNub\PNConfiguration;
use PubNub\CryptoModule;

if ($argc < 3) {
echo "Usage: php pub.php <uuid> <channel> <message> <custom_message_type>\n";
exit(1);
}

$pnUuid = $argv[1] . '-pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
$pnConfig->setUuid($pnUuid);
if (array_key_exists(4, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[4], true));
}

$pubnub = new PubNub($pnConfig);

$pubResult = $pubnub->publish()->channel($argv[2])->message($argv[3])->sync();
$pubResult = $pubnub->publish()
->channel($argv[2])
->message($argv[3])
->customMessageType($argv[4])
->sync();

printf("Published message at timetoken: %d\n", $pubResult->getTimetoken());
14 changes: 9 additions & 5 deletions examples/cli/sub.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
use PubNub\PNConfiguration;
use PubNub\CryptoModule;

if ($argc < 2) {
echo "Usage: php sub.php <channel>\n";
exit(1);
}

$pnUuid = 'pn-610da4553bb079.92567429';

$pnConfig = new PNConfiguration();
$pnConfig->setPublishKey(getenv('PN_KEY_PUBLISH'));
$pnConfig->setSubscribeKey(getenv('PN_KEY_SUBSCRIBE'));
if (array_key_exists(2, $argv)) {
$pnConfig->setCrypto(CryptoModule::aesCbcCryptor($argv[2], true));
}

$pnConfig->setUuid($pnUuid);

$pubnub = new PubNub($pnConfig);
Expand Down Expand Up @@ -58,12 +60,14 @@ public function status($pubnub, $status)
public function message($pubnub, $messageResult)
{
printf(
"\nMessage %s\n Channel: %s\n Timetoken: %s\n Publisher: %s\n",
$messageResult->getMessage(),
"\nMessage %s\n Channel: %s\n Timetoken: %s\n Publisher: %s\n Custom message type: %s\n",
json_encode($messageResult->getMessage()),
$messageResult->getChannel(),
$messageResult->getTimetoken(),
$messageResult->getPublisher(),
$messageResult->getCustomMessageType(),
);

if ($messageResult->isError()) {
printf('\nError occured during parsing the message: %s', $messageResult->getError()->getMessage());
}
Expand Down
19 changes: 19 additions & 0 deletions src/PubNub/Endpoints/FileSharing/PublishFileMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class PublishFileMessage extends FileSharingEndpoint

protected $message;
protected $meta;

/** @var string $customMessageType User defined message type */
protected ?string $customMessageType;

protected $shouldStore;
protected $ttl;

Expand All @@ -29,6 +33,17 @@ public function meta($meta)
return $this;
}

/**
* @param string $customMessageType
* @return $this
*/
public function customMessageType(?string $customMessageType)
{
$this->customMessageType = $customMessageType;

return $this;
}

public function shouldStore(bool $shouldStore)
{
$this->shouldStore = $shouldStore;
Expand Down Expand Up @@ -82,6 +97,10 @@ protected function customParams()
$params['ttl'] = $this->ttl;
$params['store'] = $this->shouldStore ? 1 : 0;

if ($this->customMessageType) {
$params['custom_message_type'] = $this->customMessageType;
}

return $params;
}

Expand Down
15 changes: 15 additions & 0 deletions src/PubNub/Endpoints/FileSharing/SendFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class SendFile extends Endpoint
protected string $fileName;
protected mixed $message;
protected mixed $meta;
protected ?string $customMessageType;
protected bool $shouldStore;
protected int $ttl;
protected mixed $fileContent;
Expand Down Expand Up @@ -51,6 +52,17 @@ public function meta($meta)
return $this;
}

/**
* @param string $customMessageType
* @return $this
*/
public function customMessageType(?string $customMessageType)
{
$this->customMessageType = $customMessageType;

return $this;
}

public function shouldStore($shouldStore)
{
$this->shouldStore = $shouldStore;
Expand Down Expand Up @@ -273,6 +285,9 @@ public function sync(): PNPublishFileMessageResult
if (isset($this->shouldStore)) {
$publishRequest->shouldStore($this->shouldStore);
}
if (isset($this->customMessageType)) {
$publishRequest->customMessageType($this->customMessageType);
}
if (isset($this->ttl)) {
$publishRequest->ttl($this->ttl);
}
Expand Down

0 comments on commit 6413a4c

Please sign in to comment.