@@ -34,25 +34,28 @@ public function __construct()
3434 * @param int $step
3535 * Creating a record in counters table with $key, $name, $inital_value, $step
3636 */
37- public function create ($ key , $ name , $ initial_value = 0 , $ step = 1 ){
37+ public function create ($ key , $ name , $ initial_value = 0 , $ step = 1 )
38+ {
3839 $ value = $ initial_value ;
39- try {
40+ try {
4041 Counter::query ()->create (
4142 compact ('key ' , 'name ' , 'initial_value ' , 'step ' , 'value ' )
4243 );
43- }catch (\Exception $ e ){
44+ } catch (\Exception $ e ) {
4445 throw CounterAlreadyExists::create ($ key );
4546 }
4647
4748 }
49+
4850 /**
4951 * @param $key
5052 * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|Counter
5153 * Get a counter object for the given $key
5254 */
53- public function get ($ key ){
55+ public function get ($ key )
56+ {
5457 $ counter = Counter::query ()->where ('key ' , $ key )->first ();
55- if (is_null ($ counter )){
58+ if (is_null ($ counter )) {
5659 throw CounterDoesNotExist::create ($ key );
5760 }
5861 return $ counter ;
@@ -65,7 +68,8 @@ public function get($key){
6568 * get the counter value for the given $key,
6669 * $default will be returned in case the key is not found
6770 */
68- public function getValue ($ key , $ default = null ){
71+ public function getValue ($ key , $ default = null )
72+ {
6973 $ counter = Counter::query ()->where ('key ' , $ key )->first ();
7074 if ($ counter ) {
7175 return $ counter ->value ;
@@ -81,7 +85,8 @@ public function getValue($key, $default = null){
8185 * @param $value
8286 * set the value of the given counter's key
8387 */
84- public function setValue ($ key , $ value ){
88+ public function setValue ($ key , $ value )
89+ {
8590 Counter::query ()->where ('key ' , $ key )->update (['value ' => $ value ]);
8691 }
8792
@@ -90,7 +95,8 @@ public function setValue($key, $value){
9095 * @param $step
9196 * set the step value for a given counter's
9297 */
93- public function setStep ($ key , $ step ){
98+ public function setStep ($ key , $ step )
99+ {
94100 Counter::query ()->where ('key ' , $ key )->update (['step ' => $ step ]);
95101
96102 }
@@ -101,11 +107,12 @@ public function setStep($key, $step){
101107 * @return \Illuminate\Database\Eloquent\Model|Counters|null
102108 * increment the counter with the step
103109 */
104- public function increment ($ key ){
110+ public function increment ($ key , $ step = null )
111+ {
105112
106113 $ counter = $ this ->get ($ key );
107- if ($ counter ){
108- $ counter ->update (['value ' => $ counter ->value + $ counter ->step ]);
114+ if ($ counter ) {
115+ $ counter ->update (['value ' => $ counter ->value + ( $ step ?? $ counter ->step ) ]);
109116 }
110117 return $ counter ;
111118 }
@@ -115,11 +122,12 @@ public function increment($key){
115122 * @return \Illuminate\Database\Eloquent\Model|Counters|null
116123 * decrement the counter with the step
117124 */
118- public function decrement ($ key ){
125+ public function decrement ($ key , $ step = null )
126+ {
119127
120128 $ counter = $ this ->get ($ key );
121- if ($ counter ){
122- $ counter ->update (['value ' => $ counter ->value - $ counter ->step ]);
129+ if ($ counter ) {
130+ $ counter ->update (['value ' => $ counter ->value - ( $ step ?? $ counter ->step ) ]);
123131 }
124132 return $ counter ;
125133 }
@@ -128,10 +136,11 @@ public function decrement($key){
128136 * @param $key
129137 * @return \Illuminate\Database\Eloquent\Model|Counters|null
130138 */
131- public function reset ($ key ){
139+ public function reset ($ key )
140+ {
132141
133142 $ counter = $ this ->get ($ key );
134- if ($ counter ){
143+ if ($ counter ) {
135144 $ counter ->update (['value ' => $ counter ->initial_value ]);
136145 }
137146 return $ counter ;
@@ -142,10 +151,11 @@ public function reset($key){
142151 * This function will store a cookie for the counter key
143152 * If the cookie already exist, the counter will not incremented again
144153 */
145- public function incrementIfNotHasCookies ($ key ){
154+ public function incrementIfNotHasCookies ($ key , $ step = null )
155+ {
146156 $ cookieName = $ this ->getCookieName ($ key );
147- if (!array_key_exists ($ cookieName , $ _COOKIE )){
148- $ this ->increment ($ key );
157+ if (!array_key_exists ($ cookieName , $ _COOKIE )) {
158+ $ this ->increment ($ key, $ step );
149159 setcookie ($ cookieName , 1 );
150160 }
151161 }
@@ -155,38 +165,47 @@ public function incrementIfNotHasCookies($key){
155165 * This function will store a cookie for the counter key
156166 * If the cookie already exist, the counter will not decremented again
157167 */
158- public function decrementIfNotHasCookies ($ key ){
168+ public function decrementIfNotHasCookies ($ key , $ step = null )
169+ {
159170 $ cookieName = $ this ->getCookieName ($ key );
160- if (!array_key_exists ($ cookieName , $ _COOKIE )){
161- $ this ->increment ($ key );
171+ if (!array_key_exists ($ cookieName , $ _COOKIE )) {
172+ $ this ->decrement ($ key, $ step );
162173 setcookie ($ cookieName , 1 );
163174 }
164175 }
165176
166-
167- /**
168- * @param $key
169- * @return string
170- */
171- private function getCookieName ($ key ){
172- return 'counters-cookie- ' . $ key ;
173- }
174-
175177 /**
176178 * @param $key
177179 * @return \Illuminate\Contracts\Routing\UrlGenerator|string
178180 * Will return the Api url to increment the counter
179181 */
180- public function getIncrementUrl ($ key ){
181- return url ("$ this ->baseUrl /increment/ " . $ key );
182+ public function getIncrementUrl ($ key , $ step = null )
183+ {
184+ $ url = url ("$ this ->baseUrl /increment/ " . $ key );
185+ if (!is_null ($ step )){
186+ $ url .= "?step= $ step " ;
187+ }
188+ return $ url ;
182189 }
183190
184191 /**
185192 * @param $key
186193 * @return \Illuminate\Contracts\Routing\UrlGenerator|string
187194 * Will return the Api url to decrement the counter
188195 */
189- public function getDecrementUrl ($ key ){
190- return url ("$ this ->baseUrl /decrement/ " . $ key );
196+ public function getDecrementUrl ($ key , $ step = null )
197+ {
198+ $ url = url ("$ this ->baseUrl /decrement/ " . $ key );
199+ if (!is_null ($ step )){
200+ $ url .= "?step= $ step " ;
201+ }
202+ return $ url ; }
203+ /**
204+ * @param $key
205+ * @return string
206+ */
207+ private function getCookieName ($ key )
208+ {
209+ return 'counters-cookie- ' . $ key ;
191210 }
192- }
211+ }
0 commit comments