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 | * 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) |
||
81 | |||
82 | /** |
||
83 | * Called when there are enough licenses available |
||
84 | * |
||
85 | * @param int $quantity |
||
86 | * @return void |
||
87 | */ |
||
88 | protected function success($quantity) |
||
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) |
||
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() |
||
114 | |||
115 | /** |
||
116 | * Returns the maximum amount of licenses |
||
117 | * |
||
118 | * @return int |
||
119 | */ |
||
120 | public function maximum() |
||
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) |
||
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) |
||
204 | |||
205 | /** |
||
206 | * Subtract licenses |
||
207 | * |
||
208 | * @param int $quantity |
||
209 | * @return boolean |
||
210 | */ |
||
211 | final public function sub($quantity = 1) |
||
230 | |||
231 | /** |
||
232 | * Set the amount of licenses |
||
233 | * |
||
234 | * @param int $quantity |
||
235 | * @return boolean |
||
236 | */ |
||
237 | final public function set($quantity) |
||
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.