Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/WalletSetDiscount.php
<?php | <?php | ||||
namespace App\Console\Commands; | namespace App\Console\Commands; | ||||
use Illuminate\Console\Command; | use App\Console\Command; | ||||
class WalletSetDiscount extends Command | class WalletSetDiscount 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:set-discount {wallet} {discount}'; | protected $signature = 'wallet:set-discount {wallet} {discount}'; | ||||
/** | /** | ||||
* The console command description. | * The console command description. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $description = 'Apply a discount to a wallet'; | protected $description = 'Apply a discount to a wallet'; | ||||
/** | /** | ||||
* Create a new command instance. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function __construct() | |||||
{ | |||||
parent::__construct(); | |||||
} | |||||
/** | |||||
* Execute the console command. | * Execute the console command. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
$wallet = \App\Wallet::where('id', $this->argument('wallet'))->first(); | $wallet = $this->getWallet($this->argument('wallet')); | ||||
if (!$wallet) { | if (!$wallet) { | ||||
return 1; | return 1; | ||||
} | } | ||||
// FIXME: Using '0' for delete might be not that obvious | // FIXME: Using '0' for delete might be not that obvious | ||||
if ($this->argument('discount') === '0') { | if ($this->argument('discount') === '0') { | ||||
$wallet->discount()->dissociate(); | $wallet->discount()->dissociate(); | ||||
} else { | } else { | ||||
$discount = \App\Discount::find($this->argument('discount')); | $discount = $this->getObject(\App\Discount::class, $this->argument('discount')); | ||||
if (!$discount) { | if (!$discount) { | ||||
return 1; | return 1; | ||||
} | } | ||||
$wallet->discount()->associate($discount); | $wallet->discount()->associate($discount); | ||||
} | } | ||||
$wallet->save(); | $wallet->save(); | ||||
} | } | ||||
} | } |