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
|
|
|
/** |
62
|
|
|
* Throws exception if there are not enough licenses available |
63
|
|
|
* |
64
|
|
|
* @param int $quantity |
65
|
|
|
* @param boolean $add |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
final public function allocate($quantity, $add = false) |
69
|
|
|
{ |
70
|
|
|
$remaining = $this->remaining(); |
71
|
|
|
if ($remaining < $quantity) { |
72
|
|
|
if(!$add) { |
73
|
|
|
$this->error($remaining, $quantity); |
74
|
|
|
} else { |
75
|
|
|
$this->add($quantity - $remaining); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->success($remaining, $quantity); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Called when there are enough licenses available |
84
|
|
|
* |
85
|
|
|
* @param int $quantity |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
protected function success($quantity) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Called when there are not enough licenses available |
95
|
|
|
* |
96
|
|
|
* @param int $remaining |
97
|
|
|
* @param int $quantity |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function error($remaining, $quantity) |
101
|
|
|
{ |
102
|
|
|
throw new LicenseException($this->message($remaining, $quantity)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the difference between the maximum amount licenses and what you are trying to limit |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function remaining() |
111
|
|
|
{ |
112
|
|
|
return max($this->maximum() - $this->used(), 0); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the maximum amount of licenses |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function maximum() |
121
|
|
|
{ |
122
|
|
|
return $this->model->quantity; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the human readable error string when there are not enough licenses available. |
127
|
|
|
* |
128
|
|
|
* @param int $remaining Number of licenses available. |
129
|
|
|
* @param int $quantity Number of licenses trying to allocate. |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function message($remaining, $quantity) |
133
|
|
|
{ |
134
|
|
|
return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available."; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns human readable string for this license |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
abstract public function name(); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the current amount of licenses in use |
146
|
|
|
* |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
|
|
abstract public function used(); |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Called before adding to the license count. |
153
|
|
|
* |
154
|
|
|
* @param int $quantity |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
abstract protected function adding($quantity); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Called after adding to the license count |
161
|
|
|
* |
162
|
|
|
* @param int $quantity |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
abstract protected function added($quantity); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Called before subtracting the license count |
169
|
|
|
* |
170
|
|
|
* @param int $quantity |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
abstract protected function subtracting($quantity); |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Called after subtracting the license count |
177
|
|
|
* |
178
|
|
|
* @param int $quantity |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
abstract protected function subtracted($quantity); |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Add more licenses |
185
|
|
|
* |
186
|
|
|
* @param int $quantity |
187
|
|
|
* @return boolean |
188
|
|
|
*/ |
189
|
|
|
final public function add($quantity = 1) |
190
|
|
|
{ |
191
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
192
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->adding($quantity); |
196
|
|
|
|
197
|
|
|
$this->model->quantity += $quantity; |
|
|
|
|
198
|
|
|
$this->model->save(); |
199
|
|
|
|
200
|
|
|
$this->added($quantity); |
201
|
|
|
|
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Subtract licenses |
207
|
|
|
* |
208
|
|
|
* @param int $quantity |
209
|
|
|
* @return boolean |
210
|
|
|
*/ |
211
|
|
|
final public function sub($quantity = 1) |
212
|
|
|
{ |
213
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
214
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if ($this->model->quantity - $quantity < 0) { |
|
|
|
|
218
|
|
|
throw new LicenseException("You cannot remove more licenses than you have available."); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->subtracting($quantity); |
222
|
|
|
|
223
|
|
|
$this->model->quantity -= $quantity; |
|
|
|
|
224
|
|
|
$this->model->save(); |
225
|
|
|
|
226
|
|
|
$this->subtracted($quantity); |
227
|
|
|
|
228
|
|
|
return true; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the amount of licenses |
233
|
|
|
* |
234
|
|
|
* @param int $quantity |
235
|
|
|
* @return boolean |
236
|
|
|
*/ |
237
|
|
|
final public function set($quantity) |
238
|
|
|
{ |
239
|
|
|
if (!is_int($quantity) || $quantity < 0) { |
240
|
|
|
throw new LicenseException("Quantity must be a positive integer."); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$difference = $quantity - $this->maximum(); |
244
|
|
|
|
245
|
|
|
if ($difference < 0) { |
246
|
|
|
return $this->sub(abs($difference)); |
247
|
|
|
} else { |
248
|
|
|
return $this->add($difference); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
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.