1 | <?php |
||
2 | namespace Turahe\Counters\Traits; |
||
3 | |||
4 | use Turahe\Counters\Models\Counter; |
||
5 | use Turahe\Counters\Facades\Counters; |
||
6 | |||
7 | /** |
||
8 | * Trait HasCounter. |
||
9 | * @package Turahe\Counters\Traits |
||
10 | */ |
||
11 | trait HasCounter |
||
12 | { |
||
13 | /** |
||
14 | * @return mixed |
||
15 | * The morph relation between any model and counters |
||
16 | */ |
||
17 | public function counters() |
||
18 | { |
||
19 | return $this->morphToMany( |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
20 | Counter::class, |
||
21 | 'counterable', |
||
22 | 'counterables' |
||
23 | )->withPivot('value', 'id'); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @param $key |
||
28 | * @return mixed |
||
29 | * Get counter related to the relation with the given $key |
||
30 | */ |
||
31 | public function getCounter($key) |
||
32 | { |
||
33 | // dd($this->counters); |
||
34 | $counter = $this->counters->where('key', $key)->first(); |
||
35 | |||
36 | //connect the counter to the object if it's not exist |
||
37 | if (! $counter) { |
||
38 | $this->addCounter($key); |
||
39 | $counter = $this->counters->where('key', $key)->first(); |
||
40 | } |
||
41 | |||
42 | return $counter; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param $key |
||
47 | * @return bool |
||
48 | * check if the related model has counter with the given key |
||
49 | */ |
||
50 | public function hasCounter($key) |
||
51 | { |
||
52 | return ! is_null($this->counters()->where('counters.key', $key)->first()); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param $key |
||
57 | * @return null |
||
58 | * Get the related model value of the counter for the given $key |
||
59 | */ |
||
60 | public function getCounterValue($key) |
||
61 | { |
||
62 | $counter = $this->getCounter($key); |
||
63 | $value = null; |
||
64 | |||
65 | if ($counter) { |
||
66 | $value = $counter->pivot->value; |
||
67 | } |
||
68 | |||
69 | return $value; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param $key |
||
74 | * @param null $initialValue |
||
0 ignored issues
–
show
|
|||
75 | * Add a record to counterable table (make relation with the given $key) |
||
76 | */ |
||
77 | public function addCounter($key, $initialValue = null) |
||
78 | { |
||
79 | $counter = Counters::get($key); |
||
80 | |||
81 | if ($counter) { |
||
82 | if (! $this->hasCounter($key)) { // not to add the counter twice |
||
83 | $this->counters()->attach( |
||
84 | $counter->id, |
||
85 | [ |
||
86 | 'value' => ! is_null($initialValue) ? $initialValue : $counter->initial_value, |
||
0 ignored issues
–
show
|
|||
87 | ] |
||
88 | ); |
||
89 | } else { |
||
90 | logger("In addCounter: This object already has counter for $key"); |
||
91 | } |
||
92 | } else { |
||
93 | logger("In addCounter: Counter Is not found for key $key"); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param $key |
||
99 | * Remove the relation in counterable table |
||
100 | */ |
||
101 | public function removeCounter($key) |
||
102 | { |
||
103 | $counter = Counters::get($key); |
||
104 | |||
105 | if ($counter) { |
||
106 | $this->counters()->detach($counter->id); |
||
107 | } else { |
||
108 | logger("In removeCounter: Counter Is not found for key $key"); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param $key |
||
114 | * @param null $step |
||
0 ignored issues
–
show
|
|||
115 | * @return mixed |
||
116 | * Increment the counterable in the relation table for the given $key |
||
117 | */ |
||
118 | public function incrementCounter($key, $step = null) |
||
119 | { |
||
120 | $counter = $this->getCounter($key); |
||
121 | |||
122 | if ($counter) { |
||
123 | $this->counters()->updateExistingPivot($counter->id, ['value' => $counter->pivot->value + ($step ?? $counter->step)]); |
||
124 | } else { |
||
125 | logger("In incrementCounter: Counter Is not found for key $key"); |
||
126 | } |
||
127 | |||
128 | return $counter; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param $key |
||
133 | * @param null $step |
||
0 ignored issues
–
show
|
|||
134 | * @return mixed |
||
135 | * Decrement the counterable in the relation table for the given $key |
||
136 | */ |
||
137 | public function decrementCounter($key, $step = null) |
||
138 | { |
||
139 | $counter = $this->getCounter($key); |
||
140 | |||
141 | if ($counter) { |
||
142 | $this->counters()->updateExistingPivot($counter->id, ['value' => $counter->pivot->value - ($step ?? $counter->step)]); |
||
143 | } else { |
||
144 | logger("In decrementCounter: Counter Is not found for key $key"); |
||
145 | } |
||
146 | |||
147 | return $counter; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param $key |
||
152 | * @param null $initalVlaue |
||
0 ignored issues
–
show
|
|||
153 | * @return mixed |
||
154 | * Reset the counterable in the relation table to the initial value for the given $key |
||
155 | */ |
||
156 | public function resetCounter($key, $initalVlaue = null) |
||
157 | { |
||
158 | $counter = $this->getCounter($key); |
||
159 | |||
160 | if ($counter) { |
||
161 | $this->counters()->updateExistingPivot($counter->id, ['value' =>$initalVlaue ?? $counter->initial_value]); |
||
162 | } else { |
||
163 | logger("In resetCounter: Counter Is not found for key $key"); |
||
164 | } |
||
165 | |||
166 | return $counter; |
||
167 | } |
||
168 | } |
||
169 |