1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\License\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
|
8
|
|
|
class LicenseUpdate extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'license:update {--quantity=?} {--license=?} {--owner_type=?} {--owner_id=?}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Updates a license quantity'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Execute the console command. |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public function handle() |
30
|
|
|
{ |
31
|
|
|
// Pick values from the database |
32
|
|
|
$owner_type_classname = $this->selectOwnerType(); |
33
|
|
|
$license_classname = $this->selectLicense(['owner_type' => $owner_type_classname]); |
34
|
|
|
$owner_id = $this->selectOwnerId(['owner_type' => $owner_type_classname, 'license' => $license_classname]); |
35
|
|
|
|
36
|
|
|
// Build the License instance |
37
|
|
|
$model = with(new $owner_type_classname)->newQuery()->where(["id" => $owner_id])->firstOrFail(); |
38
|
|
|
$license = new $license_classname($model); |
39
|
|
|
|
40
|
|
|
// Perform the update |
41
|
|
|
$license->set($this->getQuantity($license->maximum())); |
42
|
|
|
|
43
|
|
|
$this->info("Done!"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getQuantity($current = 0) |
47
|
|
|
{ |
48
|
|
|
return intval($this->option("quantity") ?: $this->ask("Please select a new maximum value for this license. (The current maximum is {$current})")); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function selectLicense($where = []) |
52
|
|
|
{ |
53
|
|
|
return $this->getSelection('license', $where); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function selectOwnerType($where = []) |
57
|
|
|
{ |
58
|
|
|
return $this->getSelection('owner_type', $where); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function selectOwnerId($where = []) |
62
|
|
|
{ |
63
|
|
|
return $this->getSelection('owner_id', $where); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
final protected function getSelection($column, $where = []) |
67
|
|
|
{ |
68
|
|
|
$selection = $this->option($column) ?: null; |
69
|
|
|
|
70
|
|
|
if (!$selection) { |
71
|
|
|
try { |
72
|
|
|
$options = DB::table('licenses')->where($where)->groupBy($column)->get([$column])->pluck($column)->toArray(); |
73
|
|
|
if(count($options) > 1) { |
74
|
|
|
$selection = $this->choice("Select a {$column}", $options); |
75
|
|
|
} else { |
76
|
|
|
$selection = $options[0]; |
77
|
|
|
} |
78
|
|
|
} catch (\OutOfBoundsException $e) { |
79
|
|
|
throw new \Exception("Could not find a {$column}", 1, $e); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->info("Selected: {$selection}"); |
84
|
|
|
|
85
|
|
|
return $selection; |
86
|
|
|
} |
87
|
|
|
} |