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 | /** |
||
62 | * Returns the owner of the license |
||
63 | * |
||
64 | * @return Model |
||
65 | */ |
||
66 | public function getOwner() |
||
70 | |||
71 | /** |
||
72 | * Throws exception if there are not enough licenses available |
||
73 | * |
||
74 | * @param int $quantity |
||
75 | * @param boolean $add |
||
76 | * @return void |
||
77 | */ |
||
78 | final public function allocate($quantity, $add = false) |
||
91 | |||
92 | /** |
||
93 | * Called when there are enough licenses available |
||
94 | * |
||
95 | * @param int $quantity |
||
96 | * @return void |
||
97 | */ |
||
98 | protected function success($quantity) |
||
102 | |||
103 | /** |
||
104 | * Called when there are not enough licenses available |
||
105 | * |
||
106 | * @param int $remaining |
||
107 | * @param int $quantity |
||
108 | * @return void |
||
109 | */ |
||
110 | protected function error($remaining, $quantity) |
||
114 | |||
115 | /** |
||
116 | * Returns the difference between the maximum amount licenses and what you are trying to limit |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | public function remaining() |
||
124 | |||
125 | /** |
||
126 | * Returns the maximum amount of licenses |
||
127 | * |
||
128 | * @return int |
||
129 | */ |
||
130 | public function maximum() |
||
134 | |||
135 | /** |
||
136 | * Returns the human readable error string when there are not enough licenses available. |
||
137 | * |
||
138 | * @param int $remaining Number of licenses available. |
||
139 | * @param int $quantity Number of licenses trying to allocate. |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function message($remaining, $quantity) |
||
146 | |||
147 | /** |
||
148 | * Returns human readable string for this license |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | abstract public function name(); |
||
153 | |||
154 | /** |
||
155 | * Returns the current amount of licenses in use |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | abstract public function used(); |
||
160 | |||
161 | /** |
||
162 | * Called before adding to the license count. |
||
163 | * |
||
164 | * @param int $quantity |
||
165 | * @return void |
||
166 | */ |
||
167 | abstract protected function adding($quantity); |
||
168 | |||
169 | /** |
||
170 | * Called after adding to the license count |
||
171 | * |
||
172 | * @param int $quantity |
||
173 | * @return void |
||
174 | */ |
||
175 | abstract protected function added($quantity); |
||
176 | |||
177 | /** |
||
178 | * Called before subtracting the license count |
||
179 | * |
||
180 | * @param int $quantity |
||
181 | * @return void |
||
182 | */ |
||
183 | abstract protected function subtracting($quantity); |
||
184 | |||
185 | /** |
||
186 | * Called after subtracting the license count |
||
187 | * |
||
188 | * @param int $quantity |
||
189 | * @return void |
||
190 | */ |
||
191 | abstract protected function subtracted($quantity); |
||
192 | |||
193 | /** |
||
194 | * Add more licenses |
||
195 | * |
||
196 | * @param int $quantity |
||
197 | * @return boolean |
||
198 | */ |
||
199 | final public function add($quantity = 1) |
||
214 | |||
215 | /** |
||
216 | * Subtract licenses |
||
217 | * |
||
218 | * @param int $quantity |
||
219 | * @return boolean |
||
220 | */ |
||
221 | final public function sub($quantity = 1) |
||
240 | |||
241 | /** |
||
242 | * Set the amount of licenses |
||
243 | * |
||
244 | * @param int $quantity |
||
245 | * @return boolean |
||
246 | */ |
||
247 | final public function set($quantity) |
||
261 | } |
||
262 |
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.