Skip to content

Commit

Permalink
Merge pull request #865 from bavix/864-can-a-new-field-be-added-to-th…
Browse files Browse the repository at this point in the history
…e-transfer-table

[11.x] Adding an Extra column to the transfer table
  • Loading branch information
rez1dent3 authored Jan 24, 2024
2 parents 7a0f9c6 + 12151a7 commit 8f57694
Show file tree
Hide file tree
Showing 27 changed files with 227 additions and 22 deletions.
30 changes: 30 additions & 0 deletions database/2024_01_24_185401_add_extra_column_in_transfer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

use Bavix\Wallet\Models\Transfer;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table($this->table(), static function (Blueprint $table) {
$table->json('extra')
->nullable()
->after('fee')
;
});
}

public function down(): void
{
Schema::dropColumns($this->table(), ['extra']);
}

private function table(): string
{
return (new Transfer())->getTable();
}
};
5 changes: 5 additions & 0 deletions phpstan.src.baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Constructor in Bavix\\\\Wallet\\\\External\\\\Dto\\\\Extra has parameter \\$extra with default value\\.$#"
count: 1
path: src/External/Dto/Extra.php

-
message: "#^Constructor in Bavix\\\\Wallet\\\\External\\\\Dto\\\\Extra has parameter \\$uuid with default value\\.$#"
count: 1
Expand Down
5 changes: 5 additions & 0 deletions src/External/Contracts/ExtraDtoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public function getDepositOption(): OptionDtoInterface;
public function getWithdrawOption(): OptionDtoInterface;

public function getUuid(): ?string;

/**
* @return array<mixed>|null
*/
public function getExtra(): ?array;
}
12 changes: 11 additions & 1 deletion src/External/Dto/Extra.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ final class Extra implements ExtraDtoInterface
/**
* @param OptionDtoInterface|array<mixed>|null $deposit
* @param OptionDtoInterface|array<mixed>|null $withdraw
* @param array<mixed>|null $extra
*/
public function __construct(
OptionDtoInterface|array|null $deposit,
OptionDtoInterface|array|null $withdraw,
private readonly ?string $uuid = null
private readonly ?string $uuid = null,
private readonly ?array $extra = null
) {
$this->deposit = $deposit instanceof OptionDtoInterface ? $deposit : new Option($deposit);
$this->withdraw = $withdraw instanceof OptionDtoInterface ? $withdraw : new Option($withdraw);
Expand All @@ -40,4 +42,12 @@ public function getUuid(): ?string
{
return $this->uuid;
}

/**
* @return array<mixed>|null
*/
public function getExtra(): ?array
{
return $this->extra;
}
}
5 changes: 4 additions & 1 deletion src/Internal/Assembler/TransferDtoAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
) {
}

/** @param array<mixed>|null $extra */
public function create(
int $depositId,
int $withdrawId,
Expand All @@ -26,7 +27,8 @@ public function create(
Model $toModel,
int $discount,
string $fee,
?string $uuid
?string $uuid,
?array $extra,
): TransferDtoInterface {
return new TransferDto(
$uuid ?? $this->uuidService->uuid4(),
Expand All @@ -37,6 +39,7 @@ public function create(
$toModel->getKey(),
$discount,
$fee,
$extra,
$this->clockService->now(),
$this->clockService->now(),
);
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Assembler/TransferDtoAssemblerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

interface TransferDtoAssemblerInterface
{
/** @param array<mixed>|null $extra */
public function create(
int $depositId,
int $withdrawId,
Expand All @@ -17,6 +18,7 @@ public function create(
Model $toModel,
int $discount,
string $fee,
?string $uuid
?string $uuid,
?array $extra,
): TransferDtoInterface;
}
16 changes: 14 additions & 2 deletions src/Internal/Assembler/TransferLazyDtoAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

final class TransferLazyDtoAssembler implements TransferLazyDtoAssemblerInterface
{
/** @param array<mixed>|null $extra */
public function create(
Wallet $fromWallet,
Wallet $toWallet,
Expand All @@ -19,8 +20,19 @@ public function create(
TransactionDtoInterface $withdrawDto,
TransactionDtoInterface $depositDto,
string $status,
?string $uuid
?string $uuid,
?array $extra,
): TransferLazyDtoInterface {
return new TransferLazyDto($fromWallet, $toWallet, $discount, $fee, $withdrawDto, $depositDto, $status, $uuid);
return new TransferLazyDto(
$fromWallet,
$toWallet,
$discount,
$fee,
$withdrawDto,
$depositDto,
$status,
$uuid,
$extra,
);
}
}
4 changes: 3 additions & 1 deletion src/Internal/Assembler/TransferLazyDtoAssemblerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

interface TransferLazyDtoAssemblerInterface
{
/** @param array<mixed>|null $extra */
public function create(
Wallet $fromWallet,
Wallet $toWallet,
Expand All @@ -18,6 +19,7 @@ public function create(
TransactionDtoInterface $withdrawDto,
TransactionDtoInterface $depositDto,
string $status,
?string $uuid
?string $uuid,
?array $extra,
): TransferLazyDtoInterface;
}
12 changes: 11 additions & 1 deletion src/Internal/Dto/BasketDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
/**
* @param non-empty-array<int|string, ItemDtoInterface> $items
* @param array<mixed> $meta
* @param array<mixed>|null $extra
*/
public function __construct(
private array $items,
private array $meta
private array $meta,
private ?array $extra,
) {
}

Expand Down Expand Up @@ -53,4 +55,12 @@ public function items(): array
{
return $this->items;
}

/**
* @return array<mixed>|null
*/
public function extra(): ?array
{
return $this->extra;
}
}
5 changes: 5 additions & 0 deletions src/Internal/Dto/BasketDtoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function total(): int;
*/
public function meta(): array;

/**
* @return array<mixed>|null
*/
public function extra(): ?array;

/**
* @return non-empty-array<int|string, ItemDtoInterface>
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Internal/Dto/TransferDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/** @immutable */
final readonly class TransferDto implements TransferDtoInterface
{
/** @param array<mixed>|null $extra */
public function __construct(
private string $uuid,
private int $depositId,
Expand All @@ -18,6 +19,7 @@ public function __construct(
private int $toId,
private int $discount,
private string $fee,
private ?array $extra,
private DateTimeImmutable $createdAt,
private DateTimeImmutable $updatedAt,
) {
Expand Down Expand Up @@ -63,6 +65,14 @@ public function getFee(): string
return $this->fee;
}

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

public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
Expand Down
3 changes: 3 additions & 0 deletions src/Internal/Dto/TransferDtoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function getDiscount(): int;

public function getFee(): string;

/** @return array<mixed>|null */
public function getExtra(): ?array;

public function getCreatedAt(): DateTimeImmutable;

public function getUpdatedAt(): DateTimeImmutable;
Expand Down
14 changes: 13 additions & 1 deletion src/Internal/Dto/TransferLazyDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
/** @immutable */
final readonly class TransferLazyDto implements TransferLazyDtoInterface
{
/**
* @param array<mixed>|null $extra
*/
public function __construct(
private Wallet $fromWallet,
private Wallet $toWallet,
Expand All @@ -17,7 +20,8 @@ public function __construct(
private TransactionDtoInterface $withdrawDto,
private TransactionDtoInterface $depositDto,
private string $status,
private ?string $uuid
private ?string $uuid,
private ?array $extra,
) {
}

Expand Down Expand Up @@ -60,4 +64,12 @@ public function getUuid(): ?string
{
return $this->uuid;
}

/**
* @return array<mixed>|null
*/
public function getExtra(): ?array
{
return $this->extra;
}
}
3 changes: 3 additions & 0 deletions src/Internal/Dto/TransferLazyDtoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public function getDepositDto(): TransactionDtoInterface;
public function getStatus(): string;

public function getUuid(): ?string;

/** @return array<mixed>|null */
public function getExtra(): ?array;
}
11 changes: 10 additions & 1 deletion src/Internal/Repository/TransferRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

use Bavix\Wallet\Internal\Dto\TransferDtoInterface;
use Bavix\Wallet\Internal\Query\TransferQueryInterface;
use Bavix\Wallet\Internal\Service\JsonServiceInterface;
use Bavix\Wallet\Internal\Transform\TransferDtoTransformerInterface;
use Bavix\Wallet\Models\Transfer;

final readonly class TransferRepository implements TransferRepositoryInterface
{
public function __construct(
private TransferDtoTransformerInterface $transformer,
private JsonServiceInterface $jsonService,
private Transfer $transfer
) {
}
Expand All @@ -22,7 +24,14 @@ public function __construct(
*/
public function insert(array $objects): void
{
$values = array_map(fn (TransferDtoInterface $dto): array => $this->transformer->extract($dto), $objects);
$values = [];
foreach ($objects as $object) {
$values[] = array_map(
fn ($value) => is_array($value) ? $this->jsonService->encode($value) : $value,
$this->transformer->extract($object)
);
}

$this->transfer->newQuery()
->insert($values)
;
Expand Down
1 change: 1 addition & 0 deletions src/Internal/Transform/TransferDtoTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function extract(TransferDtoInterface $dto): array
'to_id' => $dto->getToId(),
'discount' => $dto->getDiscount(),
'fee' => $dto->getFee(),
'extra' => $dto->getExtra(),
'created_at' => $dto->getCreatedAt(),
'updated_at' => $dto->getUpdatedAt(),
];
Expand Down
1 change: 1 addition & 0 deletions src/Internal/Transform/TransferDtoTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface TransferDtoTransformerInterface
* to_id: int|string,
* discount: int,
* fee: string,
* extra: array<mixed>|null,
* created_at: DateTimeImmutable,
* updated_at: DateTimeImmutable,
* }
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @property int $to_id
* @property string $uuid
* @property string $fee
* @property ?array<mixed> $extra
* @property Transaction $deposit
* @property Transaction $withdraw
* @property DateTimeInterface $created_at
Expand Down Expand Up @@ -59,6 +60,7 @@ class Transfer extends Model
'to_id',
'uuid',
'fee',
'extra',
'created_at',
'updated_at',
];
Expand All @@ -69,6 +71,7 @@ class Transfer extends Model
protected $casts = [
'deposit_id' => 'int',
'withdraw_id' => 'int',
'extra' => 'json',
];

public function getTable(): string
Expand Down
Loading

0 comments on commit 8f57694

Please sign in to comment.