Skip to content

Commit

Permalink
example: get individual tag
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Jan 31, 2024
1 parent 01c9efb commit df8f19a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion example/02-get-customer-by-email.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* This example finds a customer by the email address supplied as the first
* This example finds a customer by the email address, supplied as the first
* argument to the script.
*
* For any example to work, you must supply your own secrets in config.ini:
Expand Down
1 change: 1 addition & 0 deletions example/03-get-list-of-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
echo $i + 1;
echo ": ";
echo $tag->name;
echo " ($tag->id)";
echo "\n";
}
56 changes: 56 additions & 0 deletions example/04-assign-tag-to-customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* This example assigns an existing tag to an existing customer. The tag ID must
* be the first argument to the script, the customer ID must be the second
* argument to the script.
*
* For any example to work, you must supply your own secrets in config.ini:
* - username
* - client
* - secret_key
*/

use BrightFlair\SpektrixAPI\CustomerNotFoundException;
use BrightFlair\SpektrixAPI\TagNotFoundException;

chdir(dirname(__DIR__));
require "vendor/autoload.php";

$config = parse_ini_file("config.ini");
$client = new BrightFlair\SpektrixAPI\Client(
$config["username"],
$config["client"],
$config["secret_key"],
);

$tagId = $argv[1] ?? null;
if(!$tagId) {
fwrite(STDERR, "No tag ID supplied\n");
exit(1);
}

$customerId = $argv[2] ?? null;
if(!$customerId) {
fwrite(STDERR, "No customer ID supplied\n");
exit(2);
}

$tag = null;
try {
$tag = $client->getTag(id: $tagId);
}
catch(TagNotFoundException) {
fwrite(STDERR, "No tag found with ID $tagId\n");
exit(3);
}

$customer = null;
try {
$customer = $client->getCustomer(id: $customerId);
}
catch(CustomerNotFoundException) {
fwrite(STDERR, "No customer found with ID $customerId\n");
exit(4);
}

$client->addTagToCustomer($tag, $customer);
10 changes: 10 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public function getCustomer(
throw new CustomerNotFoundException($email ?? $id);
}

public function getTag(string $id):Tag {
foreach($this->getAllTags() as $tag) {
if($tag->id === $id) {
return $tag;
}
}

throw new TagNotFoundException($id);
}

/** @return array<Tag> */
public function getAllTags():array {
$endpoint = Endpoint::getAllTags;
Expand Down
4 changes: 4 additions & 0 deletions src/TagNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace BrightFlair\SpektrixAPI;

class TagNotFoundException extends SpektrixAPIException {}

0 comments on commit df8f19a

Please sign in to comment.