Skip to content

Commit 2290f82

Browse files
committed
First Version Fixes
1 parent 0b59293 commit 2290f82

File tree

4 files changed

+93
-55
lines changed

4 files changed

+93
-55
lines changed

README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@
1414
* [Special Thanks](#special-thanks)
1515
* [license](#license)
1616

17-
This package allows you to manage manage counters in the system.
17+
In some cases, you need to manage the state of the counters in your laravel project, like the number of visitors of your website,
18+
or number of view for a post, or number of downloads for a file, this needs to create a new table to save these records,
19+
or at least adding new column for your tables to save the count value.
20+
21+
This package allows you to manage counters in your laravel project.
1822

1923
Once installed you can do stuff like this:
2024

2125
```php
2226

23-
//increment/decrement system counter
27+
//increment/decrement system counters
2428
Counters::increment('number_of_visitors');
2529

2630
// increment/decrement model objects counters
2731
$post->increment('number_of_views');
2832
$feature->decrement('number_of_bugs');
2933
$user->decrement('balance', 2); // decrement the balance of the user by 2
3034
```
31-
35+
There are many other methods that are mentioned below.
3236

3337
## Installation
3438

@@ -73,8 +77,8 @@ php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" -
7377

7478
## Usage
7579
### 1) Using Counters with no models
76-
First, add the `Maher\Counters\Traits\HasCounter` trait to your `Post` model(s):
77-
80+
First, add the `Maher\Counters\Traits\HasCounter` trait to your model(s):
81+
for example we can add it to Post Model
7882
```php
7983
use Maher\Counters\Traits\HasCounter;
8084

@@ -132,11 +136,13 @@ $post->removeCounter($key);
132136

133137
//increment the counter with the given $key
134138
//Note that this will create record in countrable table,if it's not exist
135-
$post->incrementCounter($key);
139+
//if $step is entered, it will increment with the value of $step
140+
$post->incrementCounter($key, $step = null);
136141

137142
//decrement the counter with the given $key
138143
//Note that this will create record in countrable table,if it's not exist
139-
$post->decrementCounter($key);
144+
//if $step is entered, it will decrement with the value of $step
145+
$post->decrementCounter($key, $step = null);
140146

141147
// will reset the counter value (to initial_value) for the post object.
142148
$post->resetCounter($key);
@@ -146,7 +152,7 @@ $post->resetCounter($key);
146152
### 2) Using Counters with no models.
147153
Sometimes, you have general counters that are not associated with any models, for example the number visitor for your website.
148154

149-
Therefore, this package will allow you to deat with Counter with these types.
155+
Therefore, this package will allow you to deal with Counter with these types.
150156

151157
```php
152158

@@ -155,6 +161,8 @@ class Test
155161
{
156162
public function incrementFunction()
157163
{
164+
//moreover you can add this function in your public page to be incremented
165+
//every time user hits your website
158166
Counters::increment('number_of_visitors');
159167
}
160168
}
@@ -176,16 +184,16 @@ Counters::setValue($key, $value);
176184
Counters::setStep($key, $step);
177185

178186
//increment the counter with the given $key
179-
Counters::increment($key);
187+
Counters::increment($key, $step = null);
180188

181189
//decrement the counter with the given $key
182-
Counters::decrement($key);
190+
Counters::decrement($key, $step = null);
183191

184192
// will reset the counter for the inital_value
185193
Counters::reset($key);
186194
```
187195

188-
In some cases, you want to increment the counter once for every person, for example no need to increment the number_of_visitors counter everytime the same user refreshes the page.
196+
In some cases, you want to increment the counter once for every person, for example no need to increment the number_of_visitors counter every time the same user refreshes the page.
189197

190198
So you can use these functions:
191199

src/Classes/Counters.php

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

src/Facades/Counters.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@
99
/**
1010
* Class Counters
1111
* @package Maher\Counters\Facades
12-
* @method static get($key){
12+
* @method static create($key, $name, $initial_value = 0, $step = 1)
13+
* @method static get($key)
1314
* @method static getValue($key, $default = null)
1415
* @method static setValue($key, $value)
1516
* @method static setStep($key, $step)
16-
* @method static increment($key)
17-
* @method static decrement($key)
18-
* @method static incrementIfNotHasCookies($key)
19-
* @method static decrementIfNotHasCookies($key)
17+
* @method static increment($key, $step = null)
18+
* @method static decrement($key, $step = null)
19+
* @method static reset($key)
20+
* @method static incrementIfNotHasCookies($key, $step = null)
21+
* @method static decrementIfNotHasCookies($key, $step = null)
22+
* @method static getIncrementUrl($key, $step = null)
23+
* @method static getDecrementUrl($key, $step = null)
2024
*/
2125
class Counters extends Facade
2226
{
27+
28+
/**
29+
* @return string
30+
*/
2331
public static function getFacadeAccessor()
2432
{
2533
return \Maher\Counters\Classes\Counters::class;

src/Http/Controllers/CountersController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public function increment(Request $request, $counter_key)
2020
{
2121
$counter = Counters::get($counter_key);
2222

23+
$step = $request->get('step');
2324
if ($counter) {
24-
$counter = Counters::increment($counter->key);
25+
$counter = Counters::increment($counter->key, $step);
2526
return response()->json(['counter' => $counter], 200);
2627
}else{
2728
return response()->json(['counterNotFound' => true], 400);
@@ -31,8 +32,10 @@ public function increment(Request $request, $counter_key)
3132
public function decrement(Request $request, $counter_key)
3233
{
3334
$counter = Counters::get($counter_key);
35+
$step = $request->get('step');
36+
3437
if ($counter) {
35-
$counter = Counters::decrement($counter->key);
38+
$counter = Counters::decrement($counter->key, $step);
3639
return response()->json(['counter' => $counter], 200);
3740
}else{
3841
return response()->json(['counterNotFound' => true], 400);
@@ -56,7 +59,7 @@ public function decrementCounterable(Request $request, $counterable_id)
5659
$counterable = Counterable::query()->find($counterable_id);
5760
if($counterable){
5861
$counter = $counterable->counter;
59-
$counterable->update(['value' => $counterable->value + $counter->step]);
62+
$counterable->update(['value' => $counterable->value - $counter->step]);
6063
return response()->json(['counterable' => $counterable], 200);
6164
}else{
6265
return response()->json(['counter Not Found' => true], 400);

0 commit comments

Comments
 (0)