-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add write protection with examples and tests
- Loading branch information
Showing
16 changed files
with
525 additions
and
142 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,91 @@ | ||
<?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"; | ||
|
||
print("We're setting the channel's $channel additional info.\n"); | ||
print("\tTo exit type '/exit'\n"); | ||
print("\tTo show the current object type '/show'\n"); | ||
print("\tTo show this help type '/help'\n"); | ||
|
||
print("Enter the channel name: "); | ||
$name = trim(fgets(STDIN)); | ||
|
||
print("Enter the channel description: "); | ||
$description = trim(fgets(STDIN)); | ||
|
||
// Set channel metadata | ||
$pubnub->setChannelMetadata() | ||
->channel($channel) | ||
->meta([ | ||
"name" => $name, | ||
"description" => $description, | ||
]) | ||
->sync(); | ||
|
||
print("The channel has been created with name and description.\n"); | ||
|
||
while (true) { | ||
// Fetch current channel metadata | ||
$response = $pubnub->getChannelMetadata() | ||
->channel($channel) | ||
->sync(); | ||
|
||
print("Enter the field name: "); | ||
$fieldName = trim(fgets(STDIN)); | ||
|
||
if ($fieldName === '/exit') { | ||
exit(); | ||
} elseif ($fieldName === '/show') { | ||
print_r($response); | ||
continue; | ||
} elseif ($fieldName === '/help') { | ||
print("\tTo exit type '/exit'\n"); | ||
print("\tTo show the current object type '/show'\n"); | ||
print("\tTo show this help type '/help'\n"); | ||
continue; | ||
} | ||
|
||
print("Enter the field value: "); | ||
$fieldValue = trim(fgets(STDIN)); | ||
|
||
// Prepare custom fields | ||
$custom = (array)$response->getCustom(); | ||
|
||
if (isset($custom[$fieldName])) { | ||
print("Field $fieldName already has a value. Overwrite? (y/n): "); | ||
$confirmation = trim(fgets(STDIN)); | ||
|
||
if (strtolower($confirmation) !== 'y') { | ||
print("Object will not be updated.\n"); | ||
continue; | ||
} | ||
} | ||
|
||
// Update custom field | ||
$custom[$fieldName] = $fieldValue; | ||
|
||
// Writing the updated object back to the server | ||
$pubnub->setChannelMetadata() | ||
->channel($channel) | ||
->meta([ | ||
"name" => $response->getName(), | ||
"description" => $response->getDescription(), | ||
"custom" => $custom, | ||
]) | ||
->sync(); | ||
print("Object has been updated.\n"); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,66 @@ | ||
<?php | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
use PubNub\Exceptions\PubNubServerException; | ||
use PubNub\PubNub; | ||
use PubNub\PNConfiguration; | ||
|
||
$config = new PNConfiguration(); | ||
$config->setPublishKey('demo'); | ||
$config->setSubscribeKey('demo'); | ||
$config->setUuid("example"); | ||
|
||
$config_2 = clone $config; | ||
$config_2->setUuid("example_2"); | ||
|
||
$pubnub = new PubNub($config); | ||
$pubnub_2 = new PubNub($config_2); | ||
|
||
$sample_user = [ | ||
"uuid" => "SampleUser", | ||
"name" => "John Doe", | ||
"email" => "jd@example.com", | ||
"custom" => ["age" => 42, "address" => "123 Main St."] | ||
]; | ||
|
||
// One client creates a metadata for the user "SampleUser" and successfully writes it to the server. | ||
$set_result = $pubnub->setUUIDMetadata()->uuid("SampleUser")->meta($sample_user)->sync(); | ||
|
||
// We store the eTag for the user for further updates. | ||
$original_e_tag = $set_result->getETag(); | ||
|
||
print("We receive the eTag for the user: $original_e_tag" . PHP_EOL); | ||
|
||
// Another client sets the user meta with the same UUID but different data. | ||
$overwrite_result = $pubnub_2->setUUIDMetadata()->uuid("SampleUser")->meta(["name" => "Jane Doe"])->sync(); | ||
$new_e_tag = $overwrite_result->getETag(); | ||
|
||
// We can verify that there is a new eTag for the user. | ||
print( | ||
"After overwrite there's a new eTag: $original_e_tag === $new_e_tag? " | ||
. ($original_e_tag === $new_e_tag ? "true" : "false") . PHP_EOL | ||
); | ||
|
||
// We modify the user and try to update it. | ||
$updated_user = array_merge($sample_user, ["custom" => ["age" => 43, "address" => "321 Other St."]]); | ||
|
||
try { | ||
$update_result = $pubnub->setUUIDMetadata() | ||
->uuid("SampleUser") | ||
->meta($updated_user) | ||
->ifMatchesETag($original_e_tag) | ||
->sync(); | ||
print_r($update_result); | ||
} catch (PubNubServerException $e) { | ||
if ($e->getStatusCode() === 412) { | ||
print( | ||
"Update failed because eTag mismatch: " . $e->getServerErrorMessage() | ||
. "\nHTTP Status Code: " . $e->getStatusCode() . PHP_EOL | ||
); | ||
} else { | ||
print( "Unexpected error: " . $e->getMessage() . PHP_EOL); | ||
} | ||
} catch (Exception $e) { | ||
echo "Unexpected error: " . $e->getMessage() . PHP_EOL; | ||
} |
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
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,20 @@ | ||
<?php | ||
|
||
namespace PubNub\Endpoints\Objects; | ||
|
||
trait MatchesETagTrait | ||
{ | ||
protected ?string $eTag = null; | ||
protected array $customHeaders = []; | ||
|
||
/** | ||
* @param string $eTag | ||
* @return $this | ||
*/ | ||
public function ifMatchesETag(string $eTag): self | ||
{ | ||
$this->eTag = $eTag; | ||
$this->customHeaders['If-Match'] = $eTag; | ||
return $this; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -61,5 +61,4 @@ public function sort($sort) | |
|
||
return $this; | ||
} | ||
|
||
} | ||
} |
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
Oops, something went wrong.