Skip to content

Commit

Permalink
Refactor Bluesky RecordBuilder and Client for consistency and depreca…
Browse files Browse the repository at this point in the history
…tion

- Marked methods `addText()`, `addType()`, `addCreatedAt()`, `addImage()`, and `buildRecord()` in `RecordBuilder` as deprecated. Introduced alternative methods `text()`, `type()`, `createdAt()`, `image()`, and `build()` for future use.
- Updated `BlueskyClient` with deprecation notices for `setStrategy()`, `execute()`, and `getRequest()` methods, suggesting new methods to handle authentication and request processes more efficiently.
- Refactored the `urlRegex` property in `RecordBuilder` as `static` and applied consistent formatting and trigger error warnings for deprecated methods.
  • Loading branch information
shahmal1yev committed Sep 7, 2024
1 parent 7c81e3c commit 9f8d593
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 31 deletions.
78 changes: 68 additions & 10 deletions src/Builders/Bluesky/RecordBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Atproto\Builders\Bluesky;

use Atproto\Contracts\RecordBuilderContract;
use DateTimeImmutable;
use InvalidArgumentException;
use stdClass;

Expand All @@ -13,23 +14,20 @@
*/
class RecordBuilder implements RecordBuilderContract
{
/**
* @var stdClass Holds the record being built.
*/
protected $record;

/**
* @var string Regular expression pattern for matching URLs.
*/
protected $urlRegex = '/\b(?:https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i';
protected static string $urlRegex = '/\b(?:https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i';

/**
* RecordBuilder constructor.
*/
public function __construct()
{
$this->record = new stdClass();
$this->record->text = "";
$this->record = new stdClass();
$this->record->text = "";

return $this;
}
Expand All @@ -41,16 +39,23 @@ public function __construct()
*
* @return $this
* @throws InvalidArgumentException
*
* @deprecated This method deprecated and will be removed in a future version. Use `text()` instead.
*/
public function addText($text)
{
trigger_error(
"This method deprecated and will be removed in a future version. Use `text()` instead.",
E_USER_DEPRECATED
);

if (! is_string($text))
throw new InvalidArgumentException("'text' must be string");

$this->record->text = (string) $this->record->text . "$text\n";

preg_match_all(
$this->urlRegex,
self::$urlRegex,
(string) $this->record->text,
$urlMatches,
PREG_OFFSET_CAPTURE
Expand Down Expand Up @@ -82,16 +87,28 @@ public function addText($text)
return $this;
}

public function text($text)
{
return $this->addText($text);
}

/**
* Adds type to the record.
*
* @param string $type The type to be added.
*
* @return $this
* @throws InvalidArgumentException
*
* @deprecated This method deprecated and will be removed in a future version. Use `type()` instead.
*/
public function addType($type = 'app.bsky.feed.post')
{
trigger_error(
"This method deprecated and will be removed in a future version. Use `type()` instead.",
E_USER_DEPRECATED
);

if (! is_string($type))
throw new InvalidArgumentException("'type' must be string");

Expand All @@ -107,20 +124,31 @@ public function addType($type = 'app.bsky.feed.post')
return $this;
}

public function type($type = 'app.bsky.feed.post')
{
return $this->addType($type);
}

/**
* Adds creation date to the record.
*
* @param string|null $createdAt The creation date to be added.
* @param DateTimeImmutable|null $createdAt The creation date to be added.
*
* @return $this
* @throws InvalidArgumentException
*
* @deprecated This method deprecated and will be removed in a future version. Use `createdAt()` instead.
*/
public function addCreatedAt($createdAt = null)
{
trigger_error(
"This method deprecated and will be removed in a future version. Use `createdAt()` instead.",
E_USER_DEPRECATED
);

if (! is_null($createdAt))
{
if (date_create_from_format('c', $createdAt) !== false)
throw new InvalidArgumentException("'$createdAt' must be a valid date. Use 'c' format instead.");
$createdAt = $createdAt->format('c');
}
else
{
Expand All @@ -132,6 +160,11 @@ public function addCreatedAt($createdAt = null)
return $this;
}

public function createdAt(DateTimeImmutable $createdAt = null)
{
return $this->addCreatedAt($createdAt);
}

/**
* Adds image to the record.
*
Expand All @@ -140,9 +173,16 @@ public function addCreatedAt($createdAt = null)
*
* @return $this
* @throws InvalidArgumentException
*
* @deprecated This method deprecated and will be removed in a future version. Use `image()` instead.
*/
public function addImage($blob, $alt = "")
{
trigger_error(
"This method deprecated and will be removed in a future version. Use `image()` instead.",
E_USER_DEPRECATED
);

if (! is_string($alt))
throw new InvalidArgumentException("'alt' must be a string");

Expand All @@ -160,13 +200,31 @@ public function addImage($blob, $alt = "")
return $this;
}

public function image($blob, $alt = "")
{
return $this->addImage($blob, $alt);
}

/**
* Builds the record.
*
* @return stdClass The built record.
*
* @deprecated This method deprecated and will be removed in a future version. Use `build()` instead.
*/
public function buildRecord()
{
trigger_error(
"This method deprecated and will be removed in a future version. Use `build()` instead.",
E_USER_DEPRECATED
);

return $this->record;
}


public function build()
{
return $this->buildRecord();
}
}
30 changes: 17 additions & 13 deletions src/Clients/BlueskyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ class BlueskyClient implements ClientContract
public function __construct(
RequestContract $requestContract,
$url = 'https://bsky.social/xrpc'
)
{
) {
$this->url = $url;
$this->request = $requestContract;
$this->authenticated = (object) [];
$this->authStrategy = new PasswordAuthentication;
$this->authStrategy = new PasswordAuthentication();
}

/**
Expand All @@ -70,6 +69,11 @@ public function __construct(
*/
public function setStrategy(AuthStrategyContract $strategyContract)
{
trigger_error(
"This method is deprecated and will be removed in a future version. Authentication should be handled directly via `authenticate()` with credentials.",
E_USER_DEPRECATED
);

$this->authStrategy = $strategyContract;
return $this;
}
Expand Down Expand Up @@ -122,6 +126,11 @@ public function authenticate($credentials)
*/
public function execute(): object
{
trigger_error(
"This method will be renamed in the future for simplicity and to shorten method names. Use 'send()' instead.",
E_USER_DEPRECATED
);

if ($this->request->authRequired() && empty($this->authenticated)) {
throw new AuthRequired("You must be authenticated to use this method");
}
Expand Down Expand Up @@ -160,20 +169,15 @@ public function send(): object
*
* @return RequestContract The request object
*
* @deprecated This method will be renamed in the future for simplicity and to shorten method names. Use 'request()' instead.
* @deprecated This method will be removed in a future version. Directly manipulate the request object before passing it to the client. Use the appropriate request methods for setting data instead of relying on this method.
*/
public function getRequest()
{
return $this->request;
}
trigger_error(
"The 'getRequest()' method is deprecated and will be removed in a future version. Instead of using this method, please manipulate the request object directly before passing it to the client. Use the request-specific methods to set or modify data as needed.",
E_USER_DEPRECATED
);

/**
* Get the request object associated with this client.
*
* @return RequestContract $request
*/
public function request()
{
return $this->request;
}

Expand Down
48 changes: 47 additions & 1 deletion src/Contracts/RecordBuilderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Atproto\Contracts;

use DateTimeImmutable;
use InvalidArgumentException;
use stdClass;

/**
* Interface RecordBuilderContract
Expand All @@ -20,6 +22,15 @@ interface RecordBuilderContract
*/
public function addText($text);

/**
* Adds text to the record.
*
* @param string $text The text to be added.
*
* @return $this
*/
public function text($text);

/**
* Adds type to the record.
*
Expand All @@ -29,6 +40,15 @@ public function addText($text);
*/
public function addType($type);

/**
* Adds type to the record.
*
* @param string $type The type to be added.
*
* @return $this
*/
public function type($type);

/**
* Adds creation date to the record.
*
Expand All @@ -38,6 +58,15 @@ public function addType($type);
*/
public function addCreatedAt($createdAt = null);

/**
* Adds creation date to the record.
*
* @param DateTimeImmutable|null $createdAt The creation date to be added.
*
* @return $this
*/
public function createdAt(DateTimeImmutable $createdAt = null);

/**
* Adds image to the record.
*
Expand All @@ -48,10 +77,27 @@ public function addCreatedAt($createdAt = null);
*/
public function addImage($blob, $alt);

/**
* Adds image to the record.
*
* @param string $blob The image blob.
* @param string $alt The alternative text for the image.
*
* @return $this
*/
public function image($blob, $alt);

/**
* Builds the record.
*
* @return \stdClass The built record.
* @return stdClass The built record.
*/
public function buildRecord();

/**
* Builds the record.
*
* @return stdClass The built record.
*/
public function build();
}
16 changes: 9 additions & 7 deletions tests/Feature/BlueskyClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public function testAuthenticateWithInvalidCredentials()
// Test execute method with CreateRecord
public function testExecuteWithCreateRecord()
{
$request = new CreateRecord;

$recordBuilder = (new RecordBuilder())
->addText("Hello World! I am posted from PHP Unit tests for testing this URL adding to this post: \n1. https://www.fs-poster.com \n2. https://github.com/shahmal1yev/blueskysdk \n3. https://github.com/easypay/php-yigim")
->addType()
->addCreatedAt();

$request->setRecord($recordBuilder);

$client = new BlueskyClient(new CreateRecord);

$client->setStrategy(new PasswordAuthentication)
Expand All @@ -109,13 +118,6 @@ public function testExecuteWithCreateRecord()
'password' => 'ucvlqcq8'
]);

$recordBuilder = (new RecordBuilder())
->addText("Hello World! I am posted from PHP Unit tests for testing this URL adding to this post: \n1. https://www.fs-poster.com \n2. https://github.com/shahmal1yev/blueskysdk \n3. https://github.com/easypay/php-yigim")
->addType()
->addCreatedAt();

$client->getRequest()->setRecord($recordBuilder);

$response = $client->execute();

$this->assertIsObject($response);
Expand Down

0 comments on commit 9f8d593

Please sign in to comment.