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\Traits\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->owner = $owner; |
40
|
|
|
$this->model = LicenseModel::firstOrCreate([ |
41
|
|
|
"owner_type" => get_class($owner), |
42
|
|
|
"owner_id" => $owner->id, |
43
|
|
|
"license" => get_class($this), |
44
|
|
|
], [ |
45
|
|
|
"quantity" => $this->default, |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Throws exception if there are not enough licenses available |
51
|
|
|
* |
52
|
|
|
* @param int $quantity |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function check($quantity) |
56
|
|
|
{ |
57
|
|
|
$remaining = $this->remaining(); |
58
|
|
|
if ($remaining < $quantity) { |
59
|
|
|
throw new LicenseException($this->message($remaining, $quantity)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the difference between the maximum amount licenses and what you are trying to limit |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function remaining() |
69
|
|
|
{ |
70
|
|
|
return max($this->maximum() - $this->used(), 0); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the maximum amount of licenses |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function maximum() |
79
|
|
|
{ |
80
|
|
|
return $this->model->quantity; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the human readable error string when there are not enough licenses available. |
85
|
|
|
* |
86
|
|
|
* @param int $remaining Number of licenses available. |
87
|
|
|
* @param int $quantity Number of licenses trying to allocate. |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function message($remaining, $quantity) |
91
|
|
|
{ |
92
|
|
|
return "There are not enough licenses available. Tried to allocate {$quantity} but there are only {$remaining} available."; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the current amount of licenses in use |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
abstract public function used(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Called before adding to the license count. |
104
|
|
|
* |
105
|
|
|
* Should return true to proceed or false to cancel operation |
106
|
|
|
* |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
|
|
abstract protected function adding(); |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Called after adding to the license count |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
abstract protected function added(); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Called before subtracting the license count |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
abstract protected function subtracting(); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Called after subtracting the license count |
127
|
|
|
* |
128
|
|
|
* Should return true to proceed or false to cancel operation |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
abstract protected function subtracted(); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Add more licenses |
136
|
|
|
* |
137
|
|
|
* @param int $quantity |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
|
|
final public function add($quantity = 1) |
141
|
|
|
{ |
142
|
|
|
if (!is_int($quantity) || $quantity <= 0) { |
143
|
|
|
throw new LicenseExeception("Quantity must be a positive integer."); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->adding($quantity); |
|
|
|
|
147
|
|
|
|
148
|
|
|
$this->model->quantity += $quantity; |
|
|
|
|
149
|
|
|
$this->model->save(); |
150
|
|
|
|
151
|
|
|
$this->added($quantity); |
|
|
|
|
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Subtract licenses |
158
|
|
|
* |
159
|
|
|
* @param int $quantity |
160
|
|
|
* @return boolean |
161
|
|
|
*/ |
162
|
|
|
final public function sub($quantity = 1) |
163
|
|
|
{ |
164
|
|
|
if (!is_int($quantity) || $quantity <= 0) { |
165
|
|
|
throw new LicenseExeception("Quantity must be a positive integer."); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ($this->model->quantity - $quantity < 0) { |
|
|
|
|
169
|
|
|
throw new LicenseExeception("You cannot remove more licenses than you have available."); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->subtracting($quantity); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$this->model->quantity -= $quantity; |
|
|
|
|
175
|
|
|
$this->model->save(); |
176
|
|
|
|
177
|
|
|
$this->subtracted($quantity); |
|
|
|
|
178
|
|
|
|
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the amount of licenses |
184
|
|
|
* |
185
|
|
|
* @param int $quantity |
186
|
|
|
* @return boolean |
187
|
|
|
*/ |
188
|
|
|
final public function set($quantity) |
189
|
|
|
{ |
190
|
|
|
if (!is_int($quantity) || $quantity <= 0) { |
191
|
|
|
throw new LicenseExeception("Quantity must be a positive integer."); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$difference = $quantity - $this->maximum(); |
195
|
|
|
|
196
|
|
|
if ($difference < 0) { |
197
|
|
|
return $this->sub(abs($difference)); |
198
|
|
|
} else { |
199
|
|
|
return $this->add($difference); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
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.