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) { |
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
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Called when there are enough licenses available to deallocate |
131
|
|
|
* |
132
|
|
|
* @param int $quantity |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
protected function deallocateSuccess($quantity) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Called when there are not enough licenses available |
142
|
|
|
* |
143
|
|
|
* @param int $remaining |
|
|
|
|
144
|
|
|
* @param int $quantity |
|
|
|
|
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
protected function error($message) |
148
|
|
|
{ |
149
|
|
|
throw new LicenseException($message); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the difference between the maximum amount licenses and what you are trying to limit |
154
|
|
|
* |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public function remaining() |
158
|
|
|
{ |
159
|
|
|
return max($this->maximum() - $this->used(), 0); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the maximum amount of licenses |
164
|
|
|
* |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
|
|
public function maximum() |
168
|
|
|
{ |
169
|
|
|
return $this->model->quantity; |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the human readable error string when there are not enough licenses available. |
174
|
|
|
* |
175
|
|
|
* @param int $remaining Number of licenses available. |
176
|
|
|
* @param int $quantity Number of licenses trying to allocate. |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
protected function allocateMessage($remaining, $quantity) |
180
|
|
|
{ |
181
|
|
|
return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available."; |
182
|
|
|
} |
183
|
|
|
|
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
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns human readable string for this license |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
abstract public function name(); |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the current amount of licenses in use |
205
|
|
|
* |
206
|
|
|
* @return int |
207
|
|
|
*/ |
208
|
|
|
abstract public function used(); |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Called before adding to the license count. |
212
|
|
|
* |
213
|
|
|
* @param int $quantity |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
abstract protected function adding($quantity); |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Called after adding to the license count |
220
|
|
|
* |
221
|
|
|
* @param int $quantity |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
|
abstract protected function added($quantity); |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Called before subtracting the license count |
228
|
|
|
* |
229
|
|
|
* @param int $quantity |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
abstract protected function subtracting($quantity); |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Called after subtracting the license count |
236
|
|
|
* |
237
|
|
|
* @param int $quantity |
238
|
|
|
* @return void |
239
|
|
|
*/ |
240
|
|
|
abstract protected function subtracted($quantity); |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Add more licenses |
244
|
|
|
* |
245
|
|
|
* @param int $quantity |
246
|
|
|
* @return boolean |
247
|
|
|
*/ |
248
|
|
|
final public function add($quantity = 1) |
249
|
|
|
{ |
250
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
251
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->adding($quantity); |
255
|
|
|
|
256
|
|
|
$this->model->quantity += $quantity; |
|
|
|
|
257
|
|
|
$this->model->save(); |
258
|
|
|
|
259
|
|
|
$this->added($quantity); |
260
|
|
|
|
261
|
|
|
return true; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Subtract licenses |
266
|
|
|
* |
267
|
|
|
* @param int $quantity |
268
|
|
|
* @return boolean |
269
|
|
|
*/ |
270
|
|
|
final public function sub($quantity = 1) |
271
|
|
|
{ |
272
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
273
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if ($this->model->quantity - $quantity < 0) { |
|
|
|
|
277
|
|
|
throw new LicenseException("You cannot remove more licenses than you have available."); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$this->subtracting($quantity); |
281
|
|
|
|
282
|
|
|
$this->model->quantity -= $quantity; |
|
|
|
|
283
|
|
|
$this->model->save(); |
284
|
|
|
|
285
|
|
|
$this->subtracted($quantity); |
286
|
|
|
|
287
|
|
|
return true; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Set the amount of licenses |
292
|
|
|
* |
293
|
|
|
* @param int $quantity |
294
|
|
|
* @return boolean |
295
|
|
|
*/ |
296
|
|
|
final public function set($quantity) |
297
|
|
|
{ |
298
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
299
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$difference = $quantity - $this->maximum(); |
303
|
|
|
|
304
|
|
|
if ($difference < 0) { |
305
|
|
|
return $this->sub(abs($difference)); |
306
|
|
|
} else { |
307
|
|
|
return $this->add($difference); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
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.