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) |
||
48 | |||
49 | /** |
||
50 | * Throws exception if there are not enough licenses available |
||
51 | * |
||
52 | * @param int $quantity |
||
53 | * @param boolean $add |
||
54 | * @return void |
||
55 | */ |
||
56 | final public function allocate($quantity, $add = false) |
||
67 | |||
68 | /** |
||
69 | * Called when there are not enough licenses available |
||
70 | * |
||
71 | * @param [type] $remaining |
||
72 | * @param [type] $quantity |
||
73 | * @return void |
||
74 | */ |
||
75 | protected function error($remaining, $quantity) |
||
79 | |||
80 | /** |
||
81 | * Returns the difference between the maximum amount licenses and what you are trying to limit |
||
82 | * |
||
83 | * @return int |
||
84 | */ |
||
85 | public function remaining() |
||
89 | |||
90 | /** |
||
91 | * Returns the maximum amount of licenses |
||
92 | * |
||
93 | * @return int |
||
94 | */ |
||
95 | public function maximum() |
||
99 | |||
100 | /** |
||
101 | * Returns the human readable error string when there are not enough licenses available. |
||
102 | * |
||
103 | * @param int $remaining Number of licenses available. |
||
104 | * @param int $quantity Number of licenses trying to allocate. |
||
105 | * @return string |
||
106 | */ |
||
107 | protected function message($remaining, $quantity) |
||
111 | |||
112 | /** |
||
113 | * Returns human readable string for this license |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | abstract public function name(); |
||
118 | |||
119 | /** |
||
120 | * Returns the current amount of licenses in use |
||
121 | * |
||
122 | * @return int |
||
123 | */ |
||
124 | abstract public function used(); |
||
125 | |||
126 | /** |
||
127 | * Called before adding to the license count. |
||
128 | * |
||
129 | * @param int $quantity |
||
130 | * @return void |
||
131 | */ |
||
132 | abstract protected function adding($quantity); |
||
133 | |||
134 | /** |
||
135 | * Called after adding to the license count |
||
136 | * |
||
137 | * @param int $quantity |
||
138 | * @return void |
||
139 | */ |
||
140 | abstract protected function added($quantity); |
||
141 | |||
142 | /** |
||
143 | * Called before subtracting the license count |
||
144 | * |
||
145 | * @param int $quantity |
||
146 | * @return void |
||
147 | */ |
||
148 | abstract protected function subtracting($quantity); |
||
149 | |||
150 | /** |
||
151 | * Called after subtracting the license count |
||
152 | * |
||
153 | * @param int $quantity |
||
154 | * @return void |
||
155 | */ |
||
156 | abstract protected function subtracted($quantity); |
||
157 | |||
158 | /** |
||
159 | * Add more licenses |
||
160 | * |
||
161 | * @param int $quantity |
||
162 | * @return boolean |
||
163 | */ |
||
164 | final public function add($quantity = 1) |
||
179 | |||
180 | /** |
||
181 | * Subtract licenses |
||
182 | * |
||
183 | * @param int $quantity |
||
184 | * @return boolean |
||
185 | */ |
||
186 | final public function sub($quantity = 1) |
||
205 | |||
206 | /** |
||
207 | * Set the amount of licenses |
||
208 | * |
||
209 | * @param int $quantity |
||
210 | * @return boolean |
||
211 | */ |
||
212 | final public function set($quantity) |
||
226 | } |
||
227 |
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.