Skip to content

Commit

Permalink
Added *FloatQuery for High performance api handles
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jan 25, 2024
1 parent 6ac98f8 commit e83b5f1
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 6 deletions.
94 changes: 94 additions & 0 deletions src/External/Api/TransactionFloatQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\External\Api;

use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Services\CastServiceInterface;

Check failure on line 8 in src/External/Api/TransactionFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransactionFloatQuery must not depend on Bavix\Wallet\Services\CastServiceInterface (ApiQuery on ServiceInterface)
use Bavix\Wallet\Services\FormatterServiceInterface;

Check failure on line 9 in src/External/Api/TransactionFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransactionFloatQuery must not depend on Bavix\Wallet\Services\FormatterServiceInterface (ApiQuery on ServiceInterface)

final readonly class TransactionFloatQuery implements TransactionQueryInterface
{
private string $amount;

/**
* @param self::TYPE_DEPOSIT|self::TYPE_WITHDRAW $type
* @param array<mixed>|null $meta
*/
private function __construct(
private string $type,
private Wallet $wallet,
float|int|string $amount,
private ?array $meta,
private bool $confirmed,
private ?string $uuid
) {
$walletModel = app(CastServiceInterface::class)->getWallet($wallet);

Check failure on line 27 in src/External/Api/TransactionFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransactionFloatQuery must not depend on Bavix\Wallet\Services\CastServiceInterface (ApiQuery on ServiceInterface)

$this->amount = app(FormatterServiceInterface::class)

Check failure on line 29 in src/External/Api/TransactionFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransactionFloatQuery must not depend on Bavix\Wallet\Services\FormatterServiceInterface (ApiQuery on ServiceInterface)
->intValue($amount, $walletModel->decimal_places);
}

/**
* @param array<mixed>|null $meta
*/
public static function createDeposit(
Wallet $wallet,
float|int|string $amount,
?array $meta,
bool $confirmed = true,
?string $uuid = null
): self {
return new self(self::TYPE_DEPOSIT, $wallet, $amount, $meta, $confirmed, $uuid);
}

/**
* @param array<mixed>|null $meta
*/
public static function createWithdraw(
Wallet $wallet,
float|int|string $amount,
?array $meta,
bool $confirmed = true,
?string $uuid = null
): self {
return new self(self::TYPE_WITHDRAW, $wallet, $amount, $meta, $confirmed, $uuid);
}

/**
* @return self::TYPE_DEPOSIT|self::TYPE_WITHDRAW
*/
public function getType(): string
{
return $this->type;
}

public function getWallet(): Wallet
{
return $this->wallet;
}

public function getAmount(): string
{
return $this->amount;
}

/**
* @return array<mixed>|null
*/
public function getMeta(): ?array
{
return $this->meta;
}

public function isConfirmed(): bool
{
return $this->confirmed;
}

public function getUuid(): ?string
{
return $this->uuid;
}
}
53 changes: 53 additions & 0 deletions src/External/Api/TransferFloatQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\External\Api;

use Bavix\Wallet\External\Contracts\ExtraDtoInterface;
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Services\CastServiceInterface;

Check failure on line 9 in src/External/Api/TransferFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransferFloatQuery must not depend on Bavix\Wallet\Services\CastServiceInterface (ApiQuery on ServiceInterface)
use Bavix\Wallet\Services\FormatterServiceInterface;

Check failure on line 10 in src/External/Api/TransferFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransferFloatQuery must not depend on Bavix\Wallet\Services\FormatterServiceInterface (ApiQuery on ServiceInterface)

final readonly class TransferFloatQuery implements TransferQueryInterface
{
private string $amount;

/**
* @param array<mixed>|ExtraDtoInterface|null $meta
*/
public function __construct(
private Wallet $from,
private Wallet $to,
float|int|string $amount,
private array|ExtraDtoInterface|null $meta
) {
$walletModel = app(CastServiceInterface::class)->getWallet($from);

Check failure on line 25 in src/External/Api/TransferFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransferFloatQuery must not depend on Bavix\Wallet\Services\CastServiceInterface (ApiQuery on ServiceInterface)

$this->amount = app(FormatterServiceInterface::class)

Check failure on line 27 in src/External/Api/TransferFloatQuery.php

View workflow job for this annotation

GitHub Actions / deptrac

Bavix\Wallet\External\Api\TransferFloatQuery must not depend on Bavix\Wallet\Services\FormatterServiceInterface (ApiQuery on ServiceInterface)
->intValue($amount, $walletModel->decimal_places);
}

public function getFrom(): Wallet
{
return $this->from;
}

public function getTo(): Wallet
{
return $this->to;
}

public function getAmount(): string
{
return $this->amount;
}

/**
* @return array<mixed>|ExtraDtoInterface|null
*/
public function getMeta(): array|ExtraDtoInterface|null
{
return $this->meta;
}
}
9 changes: 6 additions & 3 deletions tests/Units/Api/TransactionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bavix\Wallet\Test\Units\Api;

use Bavix\Wallet\External\Api\TransactionFloatQuery;
use Bavix\Wallet\External\Api\TransactionQuery;
use Bavix\Wallet\External\Api\TransactionQueryHandlerInterface;
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
Expand Down Expand Up @@ -33,17 +34,19 @@ public function testWalletNotExists(): void
TransactionQuery::createDeposit($buyer, 100, null),
TransactionQuery::createDeposit($buyer, 100, null),
TransactionQuery::createWithdraw($buyer, 400, null),
TransactionFloatQuery::createDeposit($buyer, 2.00, null),
TransactionFloatQuery::createWithdraw($buyer, 2.00, null),
]);

self::assertSame(1, $buyer->balanceInt);
self::assertCount(5, $transactions);
self::assertCount(7, $transactions);

self::assertCount(
4,
5,
array_filter($transactions, static fn ($t) => $t->type === Transaction::TYPE_DEPOSIT),
);
self::assertCount(
1,
2,
array_filter($transactions, static fn ($t) => $t->type === Transaction::TYPE_WITHDRAW),
);
}
Expand Down
15 changes: 12 additions & 3 deletions tests/Units/Api/TransferHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bavix\Wallet\Test\Units\Api;

use Bavix\Wallet\External\Api\TransferFloatQuery;
use Bavix\Wallet\External\Api\TransferQuery;
use Bavix\Wallet\External\Api\TransferQueryHandlerInterface;
use Bavix\Wallet\External\Dto\Extra;
Expand Down Expand Up @@ -57,14 +58,22 @@ public function testWalletNotExists(): void
'7f0175fe-99cc-4058-92c6-157f0da18243',
['type' => 'third'],
)),
new TransferFloatQuery($to, $from, .50,
new Extra(
null,
null,
'1a7326a6-dfdf-4ec8-afc4-cb21cf1f43c6',
['type' => 'fourth'],
)),
]);

self::assertSame(-150, $from->balanceInt);
self::assertSame(150, $to->balanceInt);
self::assertCount(3, $transfers);
self::assertSame(-100, $from->balanceInt);
self::assertSame(100, $to->balanceInt);
self::assertCount(4, $transfers);

self::assertSame(['type' => 'first'], $transfers['598c184c-e6d6-4fc2-9640-c1c7acb38093']->extra);
self::assertSame(['type' => 'second'], $transfers['f303d60d-c2de-45d0-b9ed-e1487429709a']->extra);
self::assertSame(['type' => 'third'], $transfers['7f0175fe-99cc-4058-92c6-157f0da18243']->extra);
self::assertSame(['type' => 'fourth'], $transfers['1a7326a6-dfdf-4ec8-afc4-cb21cf1f43c6']->extra);
}
}

0 comments on commit e83b5f1

Please sign in to comment.