1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\License; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Tylercd100\License\Exceptions\LicenseException; |
7
|
|
|
use Tylercd100\License\Models\License as LicenseModel; |
8
|
|
|
use Tylercd100\License\HasLicenses; |
9
|
|
|
|
10
|
|
|
abstract class License |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Owner of the licenses |
14
|
|
|
* |
15
|
|
|
* @var Model |
16
|
|
|
*/ |
17
|
|
|
protected $owner; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The database model for tracking licenses |
21
|
|
|
* |
22
|
|
|
* @var LicenseModel |
23
|
|
|
*/ |
24
|
|
|
protected $model; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The default starting amount for the license |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $default = 0; |
32
|
|
|
|
33
|
|
|
function __construct(Model $owner) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
if (!in_array(HasLicenses::class, class_uses($owner))) { |
36
|
|
|
throw new LicenseException("The owner must use the trait: ".HasLicenses::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->model = $this->getModel($owner); |
40
|
|
|
$this->owner = $owner; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getModel(Model $owner) |
44
|
|
|
{ |
45
|
|
|
$opts = [ |
46
|
|
|
"owner_type" => get_class($owner), |
47
|
|
|
"owner_id" => $owner->id, |
48
|
|
|
"license" => get_class($this), |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$x = LicenseModel::where($opts)->first(); |
52
|
|
|
|
53
|
|
|
if (!$x) { |
54
|
|
|
$opts["quantity"] = $this->default; |
55
|
|
|
$x = LicenseModel::create($opts); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $x; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function __toString() |
62
|
|
|
{ |
63
|
|
|
return $this->name(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the owner of the license |
68
|
|
|
* |
69
|
|
|
* @return Model |
70
|
|
|
*/ |
71
|
|
|
public function getOwner() |
72
|
|
|
{ |
73
|
|
|
return $this->owner; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Throws exception if there are not enough licenses available |
78
|
|
|
* |
79
|
|
|
* @param int $quantity |
80
|
|
|
* @param boolean $add |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function allocate($quantity, $add = false) |
84
|
|
|
{ |
85
|
|
|
$remaining = $this->remaining(); |
86
|
|
|
if ($remaining < $quantity) { |
87
|
|
|
if(!$add) { |
88
|
|
|
$this->error($this->allocateMessage($remaining, $quantity)); |
89
|
|
|
} else { |
90
|
|
|
$this->add($quantity); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->allocateSuccess($quantity); |
95
|
|
|
} |
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 && $sub) { |
108
|
|
|
$this->sub($quantity); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->deallocateSuccess($quantity); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Called when there are enough licenses available to allocate |
116
|
|
|
* |
117
|
|
|
* @param int $quantity |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function allocateSuccess($quantity) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Called when there are enough licenses available to deallocate |
127
|
|
|
* |
128
|
|
|
* @param int $quantity |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
protected function deallocateSuccess($quantity) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Called when there are not enough licenses available |
138
|
|
|
* |
139
|
|
|
* @param int $remaining |
|
|
|
|
140
|
|
|
* @param int $quantity |
|
|
|
|
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function error($message) |
144
|
|
|
{ |
145
|
|
|
throw new LicenseException($message); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns the difference between the maximum amount licenses and what you are trying to limit |
150
|
|
|
* |
151
|
|
|
* @return int |
152
|
|
|
*/ |
153
|
|
|
public function remaining() |
154
|
|
|
{ |
155
|
|
|
return max($this->maximum() - $this->used(), 0); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Returns the maximum amount of licenses |
160
|
|
|
* |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
|
|
public function maximum() |
164
|
|
|
{ |
165
|
|
|
return $this->model->quantity; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the human readable error string when there are not enough licenses available. |
170
|
|
|
* |
171
|
|
|
* @param int $remaining Number of licenses available. |
172
|
|
|
* @param int $quantity Number of licenses trying to allocate. |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function allocateMessage($remaining, $quantity) |
176
|
|
|
{ |
177
|
|
|
return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available."; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns the human readable error string when there are not enough licenses remaining to remove. |
182
|
|
|
* |
183
|
|
|
* @param int $remaining Number of licenses available. |
184
|
|
|
* @param int $quantity Number of licenses trying to deallocate. |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function deallocateMessage($remaining, $quantity) |
188
|
|
|
{ |
189
|
|
|
return "You cannot remove more licenses than you have available. Tried to deallocate {$quantity} but there are only {$remaining} remaining."; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns human readable string for this license |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
abstract public function name(); |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the current amount of licenses in use |
201
|
|
|
* |
202
|
|
|
* @return int |
203
|
|
|
*/ |
204
|
|
|
abstract public function used(); |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Called before adding to the license count. |
208
|
|
|
* |
209
|
|
|
* @param int $quantity |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
abstract protected function adding($quantity); |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Called after adding to the license count |
216
|
|
|
* |
217
|
|
|
* @param int $quantity |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
abstract protected function added($quantity); |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Called before subtracting the license count |
224
|
|
|
* |
225
|
|
|
* @param int $quantity |
226
|
|
|
* @return void |
227
|
|
|
*/ |
228
|
|
|
abstract protected function subtracting($quantity); |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Called after subtracting the license count |
232
|
|
|
* |
233
|
|
|
* @param int $quantity |
234
|
|
|
* @return void |
235
|
|
|
*/ |
236
|
|
|
abstract protected function subtracted($quantity); |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Add more licenses |
240
|
|
|
* |
241
|
|
|
* @param int $quantity |
242
|
|
|
* @return boolean |
243
|
|
|
*/ |
244
|
|
|
final public function add($quantity = 1) |
245
|
|
|
{ |
246
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
247
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->adding($quantity); |
251
|
|
|
|
252
|
|
|
$this->model->quantity += $quantity; |
|
|
|
|
253
|
|
|
$this->model->save(); |
254
|
|
|
|
255
|
|
|
$this->added($quantity); |
256
|
|
|
|
257
|
|
|
return true; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Subtract licenses |
262
|
|
|
* |
263
|
|
|
* @param int $quantity |
264
|
|
|
* @return boolean |
265
|
|
|
*/ |
266
|
|
|
final public function sub($quantity = 1) |
267
|
|
|
{ |
268
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
269
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($this->model->quantity - $quantity < 0) { |
|
|
|
|
273
|
|
|
throw new LicenseException("You cannot remove more licenses than you have available."); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$this->subtracting($quantity); |
277
|
|
|
|
278
|
|
|
$this->model->quantity -= $quantity; |
|
|
|
|
279
|
|
|
$this->model->save(); |
280
|
|
|
|
281
|
|
|
$this->subtracted($quantity); |
282
|
|
|
|
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Set the amount of licenses |
288
|
|
|
* |
289
|
|
|
* @param int $quantity |
290
|
|
|
* @return boolean |
291
|
|
|
*/ |
292
|
|
|
final public function set($quantity) |
293
|
|
|
{ |
294
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
295
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$difference = $quantity - $this->maximum(); |
299
|
|
|
|
300
|
|
|
if ($difference < 0) { |
301
|
|
|
return $this->sub(abs($difference)); |
302
|
|
|
} else { |
303
|
|
|
return $this->add($difference); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.