Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ging-dev committed Sep 30, 2024
1 parent f63d951 commit 5c676f5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Api/AuthenticatedApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @extends AbstractApi<AuthenticatedSession>
*/
final class AuthenticatedApi extends AbstractApi
final class AuthenticatedApi extends AbstractApi implements AuthenticatedApiInterface
{
use LazyGhostTrait;

Expand Down Expand Up @@ -49,9 +49,9 @@ private function populateLazyProperties(): void

public function transactions(?string $accountNumber = null): TransactionBuilder
{
return new TransactionBuilder(
['accountNumber' => $accountNumber ?? $this->customer->accountNumber],
return TransactionBuilder::from(
$this->getTransactions(...),
['accountNumber' => $accountNumber ?? $this->customer->accountNumber],
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Api/AuthenticatedApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace IPay\Api;

use IPay\Builder\TransactionBuilder;

/**
* @property \IPay\Entity\Customer $customer
* @property \IPay\Entity\Account[] $accounts
*/
interface AuthenticatedApiInterface
{
public function transactions(?string $accountNumber = null): TransactionBuilder;
}
24 changes: 20 additions & 4 deletions src/Builder/TransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@
/**
* @psalm-import-type ParametersType from BodyBuilder
*
* @psalm-type GetterType = \Closure(ParametersType):\Traversable<Transaction>
*
* @implements \IteratorAggregate<int,Transaction>
*/
final class TransactionBuilder implements \IteratorAggregate
{
/**
* @param ParametersType $parameters
* @param \Closure(ParametersType):\Traversable<Transaction> $getter
* @param GetterType $getter
* @param ParametersType $parameters
*/
public function __construct(
private array $parameters,
private function __construct(
private \Closure $getter,
private array $parameters,
) {
}

/**
* @param GetterType $getter
* @param ParametersType $parameters
*/
public static function from(
\Closure $getter,
array $parameters,
): self {
return new self(
$getter,
$parameters,
);
}

public function between(
\DateTimeInterface $from,
\DateTimeInterface $to,
Expand Down
32 changes: 30 additions & 2 deletions src/Entity/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,50 @@

namespace IPay\Entity;

use EventSauce\ObjectHydrator\Constructor;

readonly class AccountState
{
public function __construct(
private function __construct(
public int $availableBalance,
public int $balance,
) {
}

#[Constructor]
public static function create(
int $availableBalance,
int $balance,
): self {
return new self(
$availableBalance,
$balance,
);
}
}

readonly class Account
{
public function __construct(
private function __construct(
public string $title,
public string $number,
public string $currencyCode,
public AccountState $accountState,
) {
}

#[Constructor]
public static function create(
string $title,
string $number,
string $currencyCode,
AccountState $accountState,
): self {
return new self(
$title,
$number,
$currencyCode,
$accountState,
);
}
}
25 changes: 17 additions & 8 deletions src/Entity/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@

namespace IPay\Entity;

use EventSauce\ObjectHydrator\MapFrom;
use EventSauce\ObjectHydrator\Constructor;

#[MapFrom([
'fullname' => 'name',
'phone' => 'phone',
'jobTitle' => 'job',
'feeAcctNo' => 'accountNumber',
])]
readonly class Customer
{
public function __construct(
private function __construct(
public string $name,
public string $phone,
public string $job,
public string $accountNumber,
) {
}

#[Constructor]
public static function create(
string $fullname,
string $phone,
string $jobTitle,
string $feeAcctNo,
): self {
return new self(
$fullname,
$phone,
$jobTitle,
$feeAcctNo,
);
}
}
26 changes: 23 additions & 3 deletions src/Entity/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@

namespace IPay\Entity;

use EventSauce\ObjectHydrator\Constructor;
use EventSauce\ObjectHydrator\PropertyCasters\CastToDateTimeImmutable;
use EventSauce\ObjectHydrator\PropertyCasters\CastToType;

readonly class Transaction
{
public function __construct(
private function __construct(
public string $currency,
#[CastToType('integer')]
public int $amount,
public string $remark,
public string $corresponsiveAccount,
public string $corresponsiveName,
#[CastToDateTimeImmutable('d-m-Y H:i:s')]
public \DateTimeImmutable $processDate,
) {
}

#[Constructor]
public static function create(
string $currency,
#[CastToType('integer')]
int $amount,
string $remark,
string $corresponsiveAccount,
string $corresponsiveName,
#[CastToDateTimeImmutable('d-m-Y H:i:s')]
\DateTimeImmutable $processDate,
): self {
return new self(
$currency,
$amount,
$remark,
$corresponsiveAccount,
$corresponsiveName,
$processDate,
);
}
}
4 changes: 2 additions & 2 deletions src/IPayClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Http\Client\Common\PluginClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use IPay\Api\AuthenticatedApi;
use IPay\Api\AuthenticatedApiInterface;
use IPay\Api\UnauthenticatedApi;
use IPay\Http\Plugin\ExceptionThrower;
use IPay\Session\UnauthenticatedSession;
Expand All @@ -19,7 +19,7 @@ final class IPayClient
/**
* @throws Exception\LoginException
*/
public static function fromCredentials(string $username, string $password): AuthenticatedApi
public static function fromCredentials(string $username, string $password): AuthenticatedApiInterface
{
$ipayClient = new self(
new HttpMethodsClient(
Expand Down

0 comments on commit 5c676f5

Please sign in to comment.