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 | * @return void |
||
54 | */ |
||
55 | public function check($quantity) |
||
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() |
||
72 | |||
73 | /** |
||
74 | * Returns the maximum amount of licenses |
||
75 | * |
||
76 | * @return int |
||
77 | */ |
||
78 | public function maximum() |
||
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) |
||
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) |
||
155 | |||
156 | /** |
||
157 | * Subtract licenses |
||
158 | * |
||
159 | * @param int $quantity |
||
160 | * @return boolean |
||
161 | */ |
||
162 | final public function sub($quantity = 1) |
||
181 | |||
182 | /** |
||
183 | * Set the amount of licenses |
||
184 | * |
||
185 | * @param int $quantity |
||
186 | * @return boolean |
||
187 | */ |
||
188 | final public function set($quantity) |
||
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.