-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de41c5e
commit f507be6
Showing
4 changed files
with
93 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Tylercd100\License\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class LicenseUpdate extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'license:update'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Updates a license quantity'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
// Pick values from the database | ||
$owner_type_classname = $this->selectOwnerType(); | ||
$license_classname = $this->selectLicense(['owner_type' => $owner_type_classname]); | ||
$owner_id = $this->selectOwnerId(['owner_type' => $owner_type_classname, 'license' => $license_classname]); | ||
|
||
// Build the License instance | ||
$model = with(new $owner_type_classname)->newQuery()->where(["id" => $owner_id])->firstOrFail(); | ||
$license = new $license_classname($model); | ||
|
||
// Perform the update | ||
$license->set($this->getQuantity($license->maximum())); | ||
|
||
$this->success("Done!"); | ||
} | ||
|
||
protected function getQuantity($current = 0) | ||
{ | ||
return intval($this->ask("Please select a new maximum value for this license. The current maximum is {$current}.")); | ||
} | ||
|
||
protected function selectLicense($where = []) | ||
{ | ||
return $this->getSelection('license', $where); | ||
} | ||
|
||
protected function selectOwnerType($where = []) | ||
{ | ||
return $this->getSelection('owner_type', $where); | ||
} | ||
|
||
protected function selectOwnerId($where = []) | ||
{ | ||
return $this->getSelection('owner_id', $where); | ||
} | ||
|
||
final protected function getSelection($column, $where = []) | ||
{ | ||
try { | ||
$options = DB::table('licenses')->where($where)->groupBy($column)->get([$column])->pluck($column); | ||
if(count($options) > 1) { | ||
$selection = $this->choice("Select a {$column}", $options); | ||
} else { | ||
$selection = $options[0]; | ||
} | ||
} catch (\OutOfBoundsException $e) { | ||
throw new \Exception("Could not find a {$column}", 1, $e); | ||
} | ||
|
||
return $selection; | ||
} | ||
} |
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