1 | <?php |
||
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) |
||
42 | |||
43 | protected function getModel(Model $owner) |
||
60 | |||
61 | public function __toString() |
||
65 | |||
66 | /** |
||
67 | * Returns the owner of the license |
||
68 | * |
||
69 | * @return Model |
||
70 | */ |
||
71 | public function getOwner() |
||
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 | final public function allocate($quantity, $add = false) |
||
96 | |||
97 | /** |
||
98 | * Called when there are enough licenses available |
||
99 | * |
||
100 | * @param int $quantity |
||
101 | * @return void |
||
102 | */ |
||
103 | protected function success($quantity) |
||
107 | |||
108 | /** |
||
109 | * Called when there are not enough licenses available |
||
110 | * |
||
111 | * @param int $remaining |
||
112 | * @param int $quantity |
||
113 | * @return void |
||
114 | */ |
||
115 | protected function error($remaining, $quantity) |
||
119 | |||
120 | /** |
||
121 | * Returns the difference between the maximum amount licenses and what you are trying to limit |
||
122 | * |
||
123 | * @return int |
||
124 | */ |
||
125 | public function remaining() |
||
129 | |||
130 | /** |
||
131 | * Returns the maximum amount of licenses |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | public function maximum() |
||
139 | |||
140 | /** |
||
141 | * Returns the human readable error string when there are not enough licenses available. |
||
142 | * |
||
143 | * @param int $remaining Number of licenses available. |
||
144 | * @param int $quantity Number of licenses trying to allocate. |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function message($remaining, $quantity) |
||
151 | |||
152 | /** |
||
153 | * Returns human readable string for this license |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | abstract public function name(); |
||
158 | |||
159 | /** |
||
160 | * Returns the current amount of licenses in use |
||
161 | * |
||
162 | * @return int |
||
163 | */ |
||
164 | abstract public function used(); |
||
165 | |||
166 | /** |
||
167 | * Called before adding to the license count. |
||
168 | * |
||
169 | * @param int $quantity |
||
170 | * @return void |
||
171 | */ |
||
172 | abstract protected function adding($quantity); |
||
173 | |||
174 | /** |
||
175 | * Called after adding to the license count |
||
176 | * |
||
177 | * @param int $quantity |
||
178 | * @return void |
||
179 | */ |
||
180 | abstract protected function added($quantity); |
||
181 | |||
182 | /** |
||
183 | * Called before subtracting the license count |
||
184 | * |
||
185 | * @param int $quantity |
||
186 | * @return void |
||
187 | */ |
||
188 | abstract protected function subtracting($quantity); |
||
189 | |||
190 | /** |
||
191 | * Called after subtracting the license count |
||
192 | * |
||
193 | * @param int $quantity |
||
194 | * @return void |
||
195 | */ |
||
196 | abstract protected function subtracted($quantity); |
||
197 | |||
198 | /** |
||
199 | * Add more licenses |
||
200 | * |
||
201 | * @param int $quantity |
||
202 | * @return boolean |
||
203 | */ |
||
204 | final public function add($quantity = 1) |
||
219 | |||
220 | /** |
||
221 | * Subtract licenses |
||
222 | * |
||
223 | * @param int $quantity |
||
224 | * @return boolean |
||
225 | */ |
||
226 | final public function sub($quantity = 1) |
||
245 | |||
246 | /** |
||
247 | * Set the amount of licenses |
||
248 | * |
||
249 | * @param int $quantity |
||
250 | * @return boolean |
||
251 | */ |
||
252 | final public function set($quantity) |
||
266 | } |
||
267 |
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.