-
-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation update and minor fixes
- Loading branch information
Showing
9 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\Wallet\Commands; | ||
|
||
use Bavix\Wallet\Models\Transfer; | ||
use Bavix\Wallet\Models\Wallet; | ||
use Bavix\Wallet\Services\CastServiceInterface; | ||
use Illuminate\Console\Command; | ||
|
||
final class TransferFixCommand extends Command | ||
{ | ||
protected $signature = 'bx:transfer:fix'; | ||
protected $description = 'Brings transfers to the correct form.'; | ||
|
||
public function handle(Wallet $wallet, Transfer $transfer, CastServiceInterface $castService): void | ||
{ | ||
$query = $transfer::with(['from', 'to']) | ||
->orWhere('from_type', '<>', $wallet->getMorphClass()) | ||
->orWhere('to_type', '<>', $wallet->getMorphClass()) | ||
; | ||
|
||
$query->each(fn (Transfer $object) => $this->fix($castService, $wallet, $object)); | ||
} | ||
|
||
private function fix(CastServiceInterface $castService, Wallet $wallet, Transfer $transfer): void | ||
{ | ||
if ($transfer->from_type !== $wallet->getMorphClass()) { | ||
$fromWallet = $castService->getWallet($transfer->from); | ||
$transfer->from_type = $fromWallet->getMorphClass(); | ||
$transfer->from_id = $fromWallet->getKey(); | ||
} | ||
|
||
if ($transfer->to_type !== $wallet->getMorphClass()) { | ||
$toWallet = $castService->getWallet($transfer->to); | ||
$transfer->to_type = $toWallet->getMorphClass(); | ||
$transfer->to_id = $toWallet->getKey(); | ||
} | ||
|
||
if ($transfer->isDirty()) { | ||
$transfer->save(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bavix\Wallet\Test\Units\Upgrade; | ||
|
||
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory; | ||
use Bavix\Wallet\Test\Infra\Models\Buyer; | ||
use Bavix\Wallet\Test\Infra\TestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class TransferFixTest extends TestCase | ||
{ | ||
public function testTransferFixCommand(): void | ||
{ | ||
/** @var Buyer $buyer1 */ | ||
/** @var Buyer $buyer2 */ | ||
[$buyer1, $buyer2] = BuyerFactory::times(2)->create(); | ||
$transfer = $buyer1->forceTransfer($buyer2, 1_000_000); | ||
|
||
self::assertSame($buyer1->wallet->getMorphClass(), $transfer->from->getMorphClass()); | ||
self::assertSame($buyer1->wallet->getKey(), $transfer->from->getKey()); | ||
|
||
self::assertSame($buyer2->wallet->getMorphClass(), $transfer->to->getMorphClass()); | ||
self::assertSame($buyer2->wallet->getKey(), $transfer->to->getKey()); | ||
|
||
$buyer1->wallet->transfers() | ||
->update([ | ||
'from_type' => $buyer1->getMorphClass(), | ||
'from_id' => $buyer1->getKey(), | ||
|
||
'to_type' => $buyer2->getMorphClass(), | ||
'to_id' => $buyer2->getKey(), | ||
]) | ||
; | ||
|
||
$transfer->refresh(); | ||
$transfer->from->refresh(); | ||
$transfer->to->refresh(); | ||
|
||
self::assertSame($buyer1->getMorphClass(), $transfer->from->getMorphClass()); | ||
self::assertSame($buyer1->getKey(), $transfer->from->getKey()); | ||
|
||
self::assertSame($buyer2->getMorphClass(), $transfer->to->getMorphClass()); | ||
self::assertSame($buyer2->getKey(), $transfer->to->getKey()); | ||
|
||
$this->artisan('bx:transfer:fix') | ||
->assertSuccessful() | ||
; | ||
|
||
$transfer->refresh(); | ||
$transfer->from->refresh(); | ||
$transfer->to->refresh(); | ||
|
||
self::assertSame($buyer1->wallet->getMorphClass(), $transfer->from->getMorphClass()); | ||
self::assertSame($buyer1->wallet->getKey(), $transfer->from->getKey()); | ||
|
||
self::assertSame($buyer2->wallet->getMorphClass(), $transfer->to->getMorphClass()); | ||
self::assertSame($buyer2->wallet->getKey(), $transfer->to->getKey()); | ||
} | ||
} |