Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/WalletCharge.php
<?php | <?php | ||||
namespace App\Console\Commands; | namespace App\Console\Commands; | ||||
use App\Wallet; | |||||
use Illuminate\Console\Command; | use Illuminate\Console\Command; | ||||
class WalletCharge extends Command | class WalletCharge extends Command | ||||
{ | { | ||||
/** | /** | ||||
* The name and signature of the console command. | * The name and signature of the console command. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $signature = 'wallet:charge'; | protected $signature = 'wallet:charge {wallet?}'; | ||||
/** | /** | ||||
* The console command description. | * The console command description. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $description = 'Charge wallets'; | protected $description = 'Charge wallets'; | ||||
Show All 9 Lines | class WalletCharge extends Command | ||||
/** | /** | ||||
* Execute the console command. | * Execute the console command. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
$wallets = \App\Wallet::all(); | if ($wallet = $this->argument('wallet')) { | ||||
// Find specified wallet by ID | |||||
$wallet = Wallet::find($wallet); | |||||
vanmeeuwen: Should we also say, something like "where number of entitlements > 0"?
Because the thing is… | |||||
Done Inline ActionsWe could, but it would not give us much improvement. I'd not do this. And I wouldn't be so sure that it's most users. machniak: We could, but it would not give us much improvement. I'd not do this.
And I wouldn't be so… | |||||
Not Done Inline ActionsFor the sake of memory consumption then, does it make sense to, instead of ->get(), do an ->each() over a closure? vanmeeuwen: For the sake of memory consumption then, does it make sense to, instead of `->get()`, do an `… | |||||
if (!$wallet || !$wallet->owner) { | |||||
return 1; | |||||
} | |||||
$wallets = [$wallet]; | |||||
} else { | |||||
// Get all wallets, excluding deleted accounts | |||||
$wallets = Wallet::join('users', 'users.id', '=', 'wallets.user_id') | |||||
->whereNull('users.deleted_at') | |||||
->get(); | |||||
} | |||||
foreach ($wallets as $wallet) { | foreach ($wallets as $wallet) { | ||||
$charge = $wallet->chargeEntitlements(); | $charge = $wallet->chargeEntitlements(); | ||||
if ($charge > 0) { | if ($charge > 0) { | ||||
$this->info( | $this->info( | ||||
"Charged wallet {$wallet->id} for user {$wallet->owner->email} with {$charge}" | "Charged wallet {$wallet->id} for user {$wallet->owner->email} with {$charge}" | ||||
); | ); | ||||
// Top-up the wallet if auto-payment enabled for the wallet | // Top-up the wallet if auto-payment enabled for the wallet | ||||
\App\Jobs\WalletCharge::dispatch($wallet); | \App\Jobs\WalletCharge::dispatch($wallet); | ||||
} | } | ||||
if ($wallet->balance < 0) { | |||||
// Check the account balance, send notifications, suspend, delete | |||||
\App\Jobs\WalletCheck::dispatch($wallet); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } |
Should we also say, something like "where number of entitlements > 0"?
Because the thing is, everyone gets a wallet, but at least currently, most users have no entitlements charged to their wallets.