Skip to content

Commit 0551e18

Browse files
authored
Merge pull request #1 from tylercd100/feature/license-commands
LicenseUpdate Command
2 parents de41c5e + f507be6 commit 0551e18

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
All notable changes to `laravel-licenser` will be documented in this file.
44

5+
### 3.5.0
6+
- Added `license:update` Command
7+
58
### 3.4.0
6-
- Added __toString() method
9+
- Added `__toString()` method
710

811
### 3.3.0
912
- Added a getter for the owner of the license

config/licenses.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
return [
44
"model" => \Tylercd100\License\Models\License::class,
5+
"command_update" => \Tylercd100\License\Commands\LicenseUpdate::class,
56
];

src/Commands/LicenseUpdate.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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';
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->success("Done!");
44+
}
45+
46+
protected function getQuantity($current = 0)
47+
{
48+
return intval($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+
try {
69+
$options = DB::table('licenses')->where($where)->groupBy($column)->get([$column])->pluck($column);
70+
if(count($options) > 1) {
71+
$selection = $this->choice("Select a {$column}", $options);
72+
} else {
73+
$selection = $options[0];
74+
}
75+
} catch (\OutOfBoundsException $e) {
76+
throw new \Exception("Could not find a {$column}", 1, $e);
77+
}
78+
79+
return $selection;
80+
}
81+
}

src/Providers/LicenseServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tylercd100\License\Providers;
44

5+
use Tylercd100\License\Commands\LicenseUpdate;
56
use Illuminate\Support\ServiceProvider;
67

78
class LicenseServiceProvider extends ServiceProvider
@@ -30,5 +31,11 @@ public function boot()
3031
__DIR__.'/../../migrations/' => database_path('migrations')
3132
], 'migrations');
3233
}
34+
35+
if ($this->app->runningInConsole()) {
36+
$this->commands([
37+
config('licenses.command_update'), // LicenseUpdate
38+
]);
39+
}
3340
}
3441
}

0 commit comments

Comments
 (0)