-
Notifications
You must be signed in to change notification settings - Fork 89
Add write protection with examples and tests #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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