Skip to content

Commit 4103d8e

Browse files
committed
Deallocation logic
1 parent 4db34d1 commit 4103d8e

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

CHANGELOG.md

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

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

5+
### 4.0.0
6+
- Deallocation logic
7+
58
### 3.5.7
69
- Added flags to command
710

src/HasLicenses.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ public function licensesAllocate($class, $quantity, $add = false)
2121
return $this;
2222
}
2323

24+
/**
25+
* Attempt to Deallocate used licenses.
26+
*
27+
* @param string $class The License class you want to work with
28+
* @param int $quantity The amount of licenses you want to attempt to use
29+
* @param boolean $add If true then it will increase the maximum available licenses
30+
* @return self
31+
*/
32+
public function licensesDeallocate($class, $quantity, $sub = false)
33+
{
34+
$license = $this->getLicenseInstance($class);
35+
$license->deallocate($quantity, $sub);
36+
return $this;
37+
}
38+
2439
/**
2540
* Returns the amount of unused licenses.
2641
*

src/License.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,59 @@ public function getOwner()
8080
* @param boolean $add
8181
* @return void
8282
*/
83-
final public function allocate($quantity, $add = false)
83+
public function allocate($quantity, $add = false)
8484
{
8585
$remaining = $this->remaining();
8686
if ($remaining < $quantity) {
8787
if(!$add) {
88-
$this->error($remaining, $quantity);
88+
$this->error($this->allocateMessage($remaining, $quantity));
8989
} else {
90-
$this->add($quantity - $remaining);
90+
$this->add($quantity);
9191
}
9292
}
9393

94-
$this->success($quantity);
94+
$this->allocateSuccess($quantity);
9595
}
96-
96+
97+
/**
98+
* Attempts to lower the quantity of licenses. The sub flag must be true.
99+
*
100+
* @param int $quantity
101+
* @param boolean $sub
102+
* @return void
103+
*/
104+
public function deallocate($quantity, $sub = false)
105+
{
106+
$used = $this->used();
107+
if ($used - $quantity >= 0) {
108+
if(!$sub) {
109+
$this->error($this->deallocateMessage($used, $quantity));
110+
} else {
111+
$this->sub($quantity);
112+
}
113+
}
114+
115+
$this->deallocateSuccess($quantity);
116+
}
117+
118+
/**
119+
* Called when there are enough licenses available to allocate
120+
*
121+
* @param int $quantity
122+
* @return void
123+
*/
124+
protected function allocateSuccess($quantity)
125+
{
126+
127+
}
128+
97129
/**
98-
* Called when there are enough licenses available
130+
* Called when there are enough licenses available to deallocate
99131
*
100132
* @param int $quantity
101133
* @return void
102134
*/
103-
protected function success($quantity)
135+
protected function deallocateSuccess($quantity)
104136
{
105137

106138
}
@@ -112,9 +144,9 @@ protected function success($quantity)
112144
* @param int $quantity
113145
* @return void
114146
*/
115-
protected function error($remaining, $quantity)
147+
protected function error($message)
116148
{
117-
throw new LicenseException($this->message($remaining, $quantity));
149+
throw new LicenseException($message);
118150
}
119151

120152
/**
@@ -144,11 +176,23 @@ public function maximum()
144176
* @param int $quantity Number of licenses trying to allocate.
145177
* @return string
146178
*/
147-
protected function message($remaining, $quantity)
179+
protected function allocateMessage($remaining, $quantity)
148180
{
149181
return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available.";
150182
}
151183

184+
/**
185+
* Returns the human readable error string when there are not enough licenses remaining to remove.
186+
*
187+
* @param int $remaining Number of licenses available.
188+
* @param int $quantity Number of licenses trying to deallocate.
189+
* @return string
190+
*/
191+
protected function deallocateMessage($remaining, $quantity)
192+
{
193+
return "You cannot remove more licenses than you have available. Tried to deallocate {$quantity} but there are only {$remaining} remaining.";
194+
}
195+
152196
/**
153197
* Returns human readable string for this license
154198
*

0 commit comments

Comments
 (0)