Skip to content

Add write protection with examples and tests#115

Merged
seba-aln merged 4 commits intomasterfrom
feat/if-matches-etag
Feb 18, 2025
Merged

Add write protection with examples and tests#115
seba-aln merged 4 commits intomasterfrom
feat/if-matches-etag

Conversation

@seba-aln
Copy link

@seba-aln seba-aln commented Feb 9, 2025

feat: Write protection with If-Match eTag header

Write protection with If-Match eTag header for setting channel and uuid metadata

$description = trim(fgets(STDIN));

// Set channel metadata
$pubnub->setChannelMetadata()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering how useful this can be for SDK user.

? What do you think about following approach:

<?php

set_time_limit(0);

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

use PubNub\PNConfiguration;
use PubNub\PubNub;

$pnconf = new PNConfiguration();
$pnconf->setPublishKey("demo");
$pnconf->setSubscribeKey("demo");
$pnconf->setUuid("example");

$pubnub = new PubNub($pnconf);

$channel = "demo_example";
$channelName = "Channel1on1";
$channelDescription = "Channel for 1on1 conversation";
$status = "active";
$type = "1on1";
$initialCustom = ["Days" => "Mon-Fri"];

// Set initial channel metadata
$pubnub->setChannelMetadata()
    ->channel($channel)
    ->name($channelName)
    ->description($channelDescription)
    ->meta(["custom" => $initialCustom])
    ->status($status)
    ->type($type)
    ->sync();

// Fetch the current metadata
$response = $pubnub->getChannelMetadata()
    ->channel($channel)
    ->includeCustom(true)
    ->sync();

$custom = (array)$response->getCustom();
$additionalMetadata = ["Months" => "Jan-May"];

// Merge additional metadata
$updatedCustomMetadata = array_merge($custom, $additionalMetadata);

// Update the channel metadata
$updatedMetadata = $pubnub->setChannelMetadata()
    ->channel($channel)
    ->custom($updatedCustomMetadata)
    ->includeCustom(true)
    ->sync();

print("Updated channel metadata:");
print_r($updatedMetadata->getData());

// Cleanup
$pubnub->removeChannelMetadata()
    ->channel($channel)
    ->sync();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add it in a different PR because it seems that this SDK misses some features and I have to look into it

* @param string $prev
* @param string $next
* @param array $data
* @param $string $eTag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:
@param ?string $eTag
?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (!empty($data))
{
$data_string = json_encode($data);
if (!empty($data)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be:

if (!empty($this->data)) {
    $data_string = json_encode($this->data);
} else {
    $data_string = 'null';
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. copypaste ;) fixed..


foreach($payload["data"] as $value)
{
foreach ($payload["data"] as $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this possible that data is null here?
If so array_push may fail?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem with that because on line 120 I create an empty array for data and if the $payload is not iterable. the worst thing may be an warning about undefined key, but I guess somewhere higher the key is always defined at least as null

Sebastian Molenda added 2 commits February 17, 2025 15:52
@seba-aln
Copy link
Author

@pubnub-release-bot release

@seba-aln seba-aln merged commit e43fbbc into master Feb 18, 2025
8 checks passed
@seba-aln seba-aln deleted the feat/if-matches-etag branch February 18, 2025 12:38
@pubnub-release-bot
Copy link
Contributor

🚀 Release successfully completed 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants