turahe /
laravel-counters
| 1 | <?php |
||
| 2 | namespace Turahe\Counters\Classes; |
||
| 3 | |||
| 4 | use Turahe\Counters\Models\Counter; |
||
| 5 | use Turahe\Counters\Exceptions\CounterDoesNotExist; |
||
| 6 | use Turahe\Counters\Exceptions\CounterAlreadyExists; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Class Counters. |
||
| 10 | * @package Turahe\Counters\Classes |
||
| 11 | */ |
||
| 12 | class Counters |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @param $key |
||
| 16 | * @param $name |
||
| 17 | * @param int $initial_value |
||
| 18 | * @param int $step |
||
| 19 | * Creating a record in counters table with $key, $name, $inital_value, $step |
||
| 20 | */ |
||
| 21 | public function create($key, $name, $initial_value = 0, $step = 1) |
||
| 22 | { |
||
| 23 | $value = $initial_value; |
||
| 24 | |||
| 25 | try { |
||
| 26 | Counter::query()->create( |
||
| 27 | compact('key', 'name', 'initial_value', 'step', 'value') |
||
| 28 | ); |
||
| 29 | } catch (\Exception $e) { |
||
| 30 | throw CounterAlreadyExists::create($key); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param $key |
||
| 36 | * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|Counter |
||
| 37 | * Get a counter object for the given $key |
||
| 38 | */ |
||
| 39 | public function get($key) |
||
| 40 | { |
||
| 41 | $counter = Counter::query()->where('key', $key)->first(); |
||
| 42 | |||
| 43 | if (is_null($counter)) { |
||
| 44 | throw CounterDoesNotExist::create($key); |
||
| 45 | } |
||
| 46 | |||
| 47 | return $counter; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param $key |
||
| 52 | * @param null $default |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 53 | * @return mixed|null|string |
||
| 54 | * get the counter value for the given $key, |
||
| 55 | * $default will be returned in case the key is not found |
||
| 56 | */ |
||
| 57 | public function getValue($key, $default = null) |
||
| 58 | { |
||
| 59 | $counter = Counter::query()->where('key', $key)->first(); |
||
| 60 | |||
| 61 | if ($counter) { |
||
| 62 | return $counter->value; |
||
| 63 | } elseif (! is_null($default)) { |
||
|
0 ignored issues
–
show
|
|||
| 64 | return $default; |
||
| 65 | } |
||
| 66 | |||
| 67 | throw CounterDoesNotExist::create($key); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param $key |
||
| 72 | * @param $value |
||
| 73 | * set the value of the given counter's key |
||
| 74 | */ |
||
| 75 | public function setValue($key, $value) |
||
| 76 | { |
||
| 77 | Counter::query()->where('key', $key)->update(['value' => $value]); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param $key |
||
| 82 | * @param $step |
||
| 83 | * set the step value for a given counter's |
||
| 84 | */ |
||
| 85 | public function setStep($key, $step) |
||
| 86 | { |
||
| 87 | Counter::query()->where('key', $key)->update(['step' => $step]); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param $key |
||
| 92 | * @param null $step |
||
|
0 ignored issues
–
show
|
|||
| 93 | * @return \Illuminate\Database\Eloquent\Model|Counters|null |
||
| 94 | * increment the counter with the step |
||
| 95 | */ |
||
| 96 | public function increment($key, $step = null) |
||
| 97 | { |
||
| 98 | $counter = $this->get($key); |
||
| 99 | |||
| 100 | if ($counter) { |
||
|
0 ignored issues
–
show
|
|||
| 101 | $counter->update(['value' => $counter->value + ($step ?? $counter->step)]); |
||
| 102 | } |
||
| 103 | |||
| 104 | return $counter; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param $key |
||
| 109 | * @param null $step |
||
|
0 ignored issues
–
show
|
|||
| 110 | * @return \Illuminate\Database\Eloquent\Model|Counters|null |
||
| 111 | * decrement the counter with the step |
||
| 112 | */ |
||
| 113 | public function decrement($key, $step = null) |
||
| 114 | { |
||
| 115 | $counter = $this->get($key); |
||
| 116 | |||
| 117 | if ($counter) { |
||
|
0 ignored issues
–
show
|
|||
| 118 | $counter->update(['value' => $counter->value - ($step ?? $counter->step)]); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $counter; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param $key |
||
| 126 | * @return \Illuminate\Database\Eloquent\Model|Counters|null |
||
| 127 | */ |
||
| 128 | public function reset($key) |
||
| 129 | { |
||
| 130 | $counter = $this->get($key); |
||
| 131 | |||
| 132 | if ($counter) { |
||
|
0 ignored issues
–
show
|
|||
| 133 | $counter->update(['value' => $counter->initial_value]); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $counter; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param $key |
||
| 141 | * This function will store a cookie for the counter key |
||
| 142 | * If the cookie already exist, the counter will not incremented again |
||
| 143 | * @param null $step |
||
|
0 ignored issues
–
show
|
|||
| 144 | */ |
||
| 145 | public function incrementIfNotHasCookies($key, $step = null) |
||
| 146 | { |
||
| 147 | $cookieName = $this->getCookieName($key); |
||
| 148 | |||
| 149 | if (! array_key_exists($cookieName, $_COOKIE)) { |
||
| 150 | $this->increment($key, $step); |
||
| 151 | setcookie($cookieName, 1); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param $key |
||
| 157 | * This function will store a cookie for the counter key |
||
| 158 | * If the cookie already exist, the counter will not decremented again |
||
| 159 | * @param null $step |
||
|
0 ignored issues
–
show
|
|||
| 160 | */ |
||
| 161 | public function decrementIfNotHasCookies($key, $step = null) |
||
| 162 | { |
||
| 163 | $cookieName = $this->getCookieName($key); |
||
| 164 | |||
| 165 | if (! array_key_exists($cookieName, $_COOKIE)) { |
||
| 166 | $this->decrement($key, $step); |
||
| 167 | setcookie($cookieName, 1); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param $key |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | private function getCookieName($key) |
||
| 176 | { |
||
| 177 | return 'counters-cookie-'.$key; |
||
| 178 | } |
||
| 179 | } |
||
| 180 |