Skip to content

Commit

Permalink
feat: better DX
Browse files Browse the repository at this point in the history
  • Loading branch information
ging-dev committed Aug 14, 2024
1 parent 9ba6b92 commit f465396
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use IPay\IPayClient;

require __DIR__.'/vendor/autoload.php';

$ipay = IPayClient::create();

try {
$session = $ipay->login($username, $password);
$session = IPayClient::fromCredentials('username', 'password');

$transactions = $session->transactions()
->type(TransactionType::CREDIT)
Expand Down
24 changes: 11 additions & 13 deletions src/IPayClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

final class IPayClient
{
public static function create(): static
/**
* @throws Exception\LoginException
*/
public static function fromCredentials(string $username, string $password): AuthenticatedApi
{
return new self(
$ipayClient = new self(
new HttpMethodsClient(
new PluginClient(Psr18ClientDiscovery::find(), [
new BaseUriPlugin(Psr17FactoryDiscovery::findUriFactory()
->createUri('https://api-ipay.vietinbank.vn')
new BaseUriPlugin(
Psr17FactoryDiscovery::findUriFactory()
->createUri('https://api-ipay.vietinbank.vn')
),
new ContentTypePlugin(),
new ExceptionThrower(),
Expand All @@ -31,6 +35,9 @@ public static function create(): static
Psr17FactoryDiscovery::findStreamFactory(),
),
);

return (new UnauthenticatedApi($ipayClient, new UnauthenticatedSession()))
->login($username, $password);
}

private function __construct(
Expand All @@ -42,13 +49,4 @@ public function getClient(): HttpMethodsClientInterface
{
return $this->client;
}

/**
* @throws Exception\LoginException
*/
public function login(string $userName, string $accessCode): AuthenticatedApi
{
return (new UnauthenticatedApi($this, new UnauthenticatedSession()))
->login($userName, $accessCode);
}
}

0 comments on commit f465396

Please sign in to comment.