@@ -80,27 +80,59 @@ public function getOwner()
8080 * @param boolean $add
8181 * @return void
8282 */
83- final public function allocate ($ quantity , $ add = false )
83+ public function allocate ($ quantity , $ add = false )
8484 {
8585 $ remaining = $ this ->remaining ();
8686 if ($ remaining < $ quantity ) {
8787 if (!$ add ) {
88- $ this ->error ($ remaining , $ quantity );
88+ $ this ->error ($ this -> allocateMessage ( $ remaining , $ quantity) );
8989 } else {
90- $ this ->add ($ quantity - $ remaining );
90+ $ this ->add ($ quantity );
9191 }
9292 }
9393
94- $ this ->success ($ quantity );
94+ $ this ->allocateSuccess ($ quantity );
9595 }
96-
96+
97+ /**
98+ * Attempts to lower the quantity of licenses. The sub flag must be true.
99+ *
100+ * @param int $quantity
101+ * @param boolean $sub
102+ * @return void
103+ */
104+ public function deallocate ($ quantity , $ sub = false )
105+ {
106+ $ used = $ this ->used ();
107+ if ($ used - $ quantity >= 0 ) {
108+ if (!$ sub ) {
109+ $ this ->error ($ this ->deallocateMessage ($ used , $ quantity ));
110+ } else {
111+ $ this ->sub ($ quantity );
112+ }
113+ }
114+
115+ $ this ->deallocateSuccess ($ quantity );
116+ }
117+
118+ /**
119+ * Called when there are enough licenses available to allocate
120+ *
121+ * @param int $quantity
122+ * @return void
123+ */
124+ protected function allocateSuccess ($ quantity )
125+ {
126+
127+ }
128+
97129 /**
98- * Called when there are enough licenses available
130+ * Called when there are enough licenses available to deallocate
99131 *
100132 * @param int $quantity
101133 * @return void
102134 */
103- protected function success ($ quantity )
135+ protected function deallocateSuccess ($ quantity )
104136 {
105137
106138 }
@@ -112,9 +144,9 @@ protected function success($quantity)
112144 * @param int $quantity
113145 * @return void
114146 */
115- protected function error ($ remaining , $ quantity )
147+ protected function error ($ message )
116148 {
117- throw new LicenseException ($ this -> message ( $ remaining , $ quantity ) );
149+ throw new LicenseException ($ message );
118150 }
119151
120152 /**
@@ -144,11 +176,23 @@ public function maximum()
144176 * @param int $quantity Number of licenses trying to allocate.
145177 * @return string
146178 */
147- protected function message ($ remaining , $ quantity )
179+ protected function allocateMessage ($ remaining , $ quantity )
148180 {
149181 return "There are not enough licenses available. Tried to allocate {$ quantity } but there are only {$ remaining } available. " ;
150182 }
151183
184+ /**
185+ * Returns the human readable error string when there are not enough licenses remaining to remove.
186+ *
187+ * @param int $remaining Number of licenses available.
188+ * @param int $quantity Number of licenses trying to deallocate.
189+ * @return string
190+ */
191+ protected function deallocateMessage ($ remaining , $ quantity )
192+ {
193+ return "You cannot remove more licenses than you have available. Tried to deallocate {$ quantity } but there are only {$ remaining } remaining. " ;
194+ }
195+
152196 /**
153197 * Returns human readable string for this license
154198 *
0 commit comments