diff --git a/CHANGELOG.md b/CHANGELOG.md index 65fa83e..6dbe6bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to `laravel-licenser` will be documented in this file. +### 4.0.0 +- Deallocation logic + ### 3.5.7 - Added flags to command diff --git a/src/HasLicenses.php b/src/HasLicenses.php index 7b2bf0c..017b5e8 100644 --- a/src/HasLicenses.php +++ b/src/HasLicenses.php @@ -21,6 +21,21 @@ public function licensesAllocate($class, $quantity, $add = false) return $this; } + /** + * Attempt to Deallocate used licenses. + * + * @param string $class The License class you want to work with + * @param int $quantity The amount of licenses you want to attempt to use + * @param boolean $add If true then it will increase the maximum available licenses + * @return self + */ + public function licensesDeallocate($class, $quantity, $sub = false) + { + $license = $this->getLicenseInstance($class); + $license->deallocate($quantity, $sub); + return $this; + } + /** * Returns the amount of unused licenses. * diff --git a/src/License.php b/src/License.php index 84bd4fd..2e7f07b 100644 --- a/src/License.php +++ b/src/License.php @@ -80,27 +80,59 @@ public function getOwner() * @param boolean $add * @return void */ - final public function allocate($quantity, $add = false) + public function allocate($quantity, $add = false) { $remaining = $this->remaining(); if ($remaining < $quantity) { if(!$add) { - $this->error($remaining, $quantity); + $this->error($this->allocateMessage($remaining, $quantity)); } else { - $this->add($quantity - $remaining); + $this->add($quantity); } } - $this->success($quantity); + $this->allocateSuccess($quantity); } - + + /** + * Attempts to lower the quantity of licenses. The sub flag must be true. + * + * @param int $quantity + * @param boolean $sub + * @return void + */ + public function deallocate($quantity, $sub = false) + { + $used = $this->used(); + if ($used - $quantity >= 0) { + if(!$sub) { + $this->error($this->deallocateMessage($used, $quantity)); + } else { + $this->sub($quantity); + } + } + + $this->deallocateSuccess($quantity); + } + + /** + * Called when there are enough licenses available to allocate + * + * @param int $quantity + * @return void + */ + protected function allocateSuccess($quantity) + { + + } + /** - * Called when there are enough licenses available + * Called when there are enough licenses available to deallocate * * @param int $quantity * @return void */ - protected function success($quantity) + protected function deallocateSuccess($quantity) { } @@ -112,9 +144,9 @@ protected function success($quantity) * @param int $quantity * @return void */ - protected function error($remaining, $quantity) + protected function error($message) { - throw new LicenseException($this->message($remaining, $quantity)); + throw new LicenseException($message); } /** @@ -144,11 +176,23 @@ public function maximum() * @param int $quantity Number of licenses trying to allocate. * @return string */ - protected function message($remaining, $quantity) + protected function allocateMessage($remaining, $quantity) { return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available."; } + /** + * Returns the human readable error string when there are not enough licenses remaining to remove. + * + * @param int $remaining Number of licenses available. + * @param int $quantity Number of licenses trying to deallocate. + * @return string + */ + protected function deallocateMessage($remaining, $quantity) + { + return "You cannot remove more licenses than you have available. Tried to deallocate {$quantity} but there are only {$remaining} remaining."; + } + /** * Returns human readable string for this license *