Skip to content

Commit

Permalink
Merge pull request #1 from tylercd100/feature/license-commands
Browse files Browse the repository at this point in the history
LicenseUpdate Command
  • Loading branch information
tylercd100 committed Mar 13, 2018
2 parents de41c5e + f507be6 commit 0551e18
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/licenses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
"model" => \Tylercd100\License\Models\License::class,
"command_update" => \Tylercd100\License\Commands\LicenseUpdate::class,
];
81 changes: 81 additions & 0 deletions src/Commands/LicenseUpdate.php
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;
}
}
7 changes: 7 additions & 0 deletions src/Providers/LicenseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tylercd100\License\Providers;

use Tylercd100\License\Commands\LicenseUpdate;
use Illuminate\Support\ServiceProvider;

class LicenseServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -30,5 +31,11 @@ public function boot()
__DIR__.'/../../migrations/' => database_path('migrations')
], 'migrations');
}

if ($this->app->runningInConsole()) {
$this->commands([
config('licenses.command_update'), // LicenseUpdate
]);
}
}
}

0 comments on commit 0551e18

Please sign in to comment.