Skip to content

Commit

Permalink
Deallocation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercd100 committed Aug 21, 2018
1 parent 4db34d1 commit 4103d8e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions src/HasLicenses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
64 changes: 54 additions & 10 deletions src/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 4103d8e

Please sign in to comment.