diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa7aa1..98f9364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,11 @@ All notable changes to `laravel-licenser` will be documented in this file. +### 3.5.0 +- Added `license:update` Command + ### 3.4.0 -- Added __toString() method +- Added `__toString()` method ### 3.3.0 - Added a getter for the owner of the license diff --git a/config/licenses.php b/config/licenses.php index 3e39cb0..f6a660a 100644 --- a/config/licenses.php +++ b/config/licenses.php @@ -2,4 +2,5 @@ return [ "model" => \Tylercd100\License\Models\License::class, + "command_update" => \Tylercd100\License\Commands\LicenseUpdate::class, ]; diff --git a/src/Commands/LicenseUpdate.php b/src/Commands/LicenseUpdate.php new file mode 100644 index 0000000..4ebee3b --- /dev/null +++ b/src/Commands/LicenseUpdate.php @@ -0,0 +1,81 @@ +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; + } +} \ No newline at end of file diff --git a/src/Providers/LicenseServiceProvider.php b/src/Providers/LicenseServiceProvider.php index 5bbfbca..41d28ec 100644 --- a/src/Providers/LicenseServiceProvider.php +++ b/src/Providers/LicenseServiceProvider.php @@ -2,6 +2,7 @@ namespace Tylercd100\License\Providers; +use Tylercd100\License\Commands\LicenseUpdate; use Illuminate\Support\ServiceProvider; class LicenseServiceProvider extends ServiceProvider @@ -30,5 +31,11 @@ public function boot() __DIR__.'/../../migrations/' => database_path('migrations') ], 'migrations'); } + + if ($this->app->runningInConsole()) { + $this->commands([ + config('licenses.command_update'), // LicenseUpdate + ]); + } } } \ No newline at end of file