1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2017 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic, Inc. |
||
6 | * |
||
7 | * @link https://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\CampaignBundle\Executioner\ContactFinder\Limiter; |
||
13 | |||
14 | use Mautic\CampaignBundle\Executioner\Exception\NoContactsFoundException; |
||
15 | |||
16 | /** |
||
17 | * Class ContactLimiter. |
||
18 | */ |
||
19 | class ContactLimiter |
||
20 | { |
||
21 | /** |
||
22 | * @var int|null |
||
23 | */ |
||
24 | private $batchLimit; |
||
25 | |||
26 | /** |
||
27 | * @var int|null |
||
28 | */ |
||
29 | private $contactId; |
||
30 | |||
31 | /** |
||
32 | * @var int|null |
||
33 | */ |
||
34 | private $minContactId; |
||
35 | |||
36 | /** |
||
37 | * @var int|null |
||
38 | */ |
||
39 | private $batchMinContactId; |
||
40 | |||
41 | /** |
||
42 | * @var int|null |
||
43 | */ |
||
44 | private $maxContactId; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $contactIdList; |
||
50 | |||
51 | /** |
||
52 | * @var int|null |
||
53 | */ |
||
54 | private $threadId; |
||
55 | |||
56 | /** |
||
57 | * @var int|null |
||
58 | */ |
||
59 | private $maxThreads; |
||
60 | |||
61 | /** |
||
62 | * @var int|null |
||
63 | */ |
||
64 | private $campaignLimit; |
||
65 | |||
66 | /** |
||
67 | * @var int|null |
||
68 | */ |
||
69 | private $campaignLimitUsed; |
||
70 | |||
71 | /** |
||
72 | * ContactLimiter constructor. |
||
73 | * |
||
74 | * @param int $batchLimit |
||
75 | * @param int|null $contactId |
||
76 | * @param int|null $minContactId |
||
77 | * @param int|null $maxContactId |
||
78 | * @param int|null $threadId |
||
79 | * @param int|null $maxThreads |
||
80 | * @param int|null $campaignLimit |
||
81 | */ |
||
82 | public function __construct( |
||
83 | $batchLimit, |
||
84 | $contactId = null, |
||
85 | $minContactId = null, |
||
86 | $maxContactId = null, |
||
87 | array $contactIdList = [], |
||
88 | $threadId = null, |
||
89 | $maxThreads = null, |
||
90 | $campaignLimit = null |
||
91 | ) { |
||
92 | $this->batchLimit = ($batchLimit) ? (int) $batchLimit : 100; |
||
93 | $this->contactId = ($contactId) ? (int) $contactId : null; |
||
94 | $this->minContactId = ($minContactId) ? (int) $minContactId : null; |
||
95 | $this->maxContactId = ($maxContactId) ? (int) $maxContactId : null; |
||
96 | $this->contactIdList = $contactIdList; |
||
97 | |||
98 | if ($threadId && $maxThreads) { |
||
99 | $this->threadId = (int) $threadId; |
||
100 | $this->maxThreads = (int) $maxThreads; |
||
101 | |||
102 | if ($threadId > $maxThreads) { |
||
103 | throw new \InvalidArgumentException('$threadId cannot be larger than $maxThreads'); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | if ($campaignLimit) { |
||
0 ignored issues
–
show
|
|||
108 | $this->campaignLimit = $campaignLimit; |
||
109 | $this->campaignLimitUsed = 0; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return int |
||
115 | */ |
||
116 | public function getBatchLimit() |
||
117 | { |
||
118 | return $this->batchLimit; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return int|null |
||
123 | */ |
||
124 | public function getContactId() |
||
125 | { |
||
126 | return $this->contactId; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return int|null |
||
131 | */ |
||
132 | public function getMinContactId() |
||
133 | { |
||
134 | return ($this->batchMinContactId) ? $this->batchMinContactId : $this->minContactId; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return int|null |
||
139 | */ |
||
140 | public function getMaxContactId() |
||
141 | { |
||
142 | return $this->maxContactId; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getContactIdList() |
||
149 | { |
||
150 | return $this->contactIdList; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param int $id |
||
155 | * |
||
156 | * @return $this |
||
157 | * |
||
158 | * @throws NoContactsFoundException |
||
159 | */ |
||
160 | public function setBatchMinContactId($id) |
||
161 | { |
||
162 | // Prevent a never ending loop if the contact ID never changes due to being the last batch of contacts |
||
163 | if ($this->minContactId && $this->minContactId > (int) $id) { |
||
164 | throw new NoContactsFoundException(); |
||
165 | } |
||
166 | |||
167 | // We've surpasssed the max so bai |
||
168 | if ($this->maxContactId && $this->maxContactId < (int) $id) { |
||
169 | throw new NoContactsFoundException(); |
||
170 | } |
||
171 | |||
172 | // The same batch of contacts were somehow processed so let's stop to prevent the loop |
||
173 | if ($this->batchMinContactId && $this->batchMinContactId >= $id) { |
||
174 | throw new NoContactsFoundException(); |
||
175 | } |
||
176 | |||
177 | $this->batchMinContactId = (int) $id; |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function resetBatchMinContactId() |
||
186 | { |
||
187 | $this->batchMinContactId = null; |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return int|null |
||
194 | */ |
||
195 | public function getMaxThreads() |
||
196 | { |
||
197 | return $this->maxThreads; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @return int|null |
||
202 | */ |
||
203 | public function getThreadId() |
||
204 | { |
||
205 | return $this->threadId; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @return int|null |
||
210 | */ |
||
211 | public function getCampaignLimit() |
||
212 | { |
||
213 | return $this->campaignLimit; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function hasCampaignLimit() |
||
220 | { |
||
221 | return null !== $this->campaignLimit; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @return int |
||
226 | * |
||
227 | * @throws \Exception |
||
228 | */ |
||
229 | public function getCampaignLimitRemaining() |
||
230 | { |
||
231 | if (!$this->hasCampaignLimit()) { |
||
232 | throw new \Exception('Campaign Limit was not set'); |
||
233 | } |
||
234 | |||
235 | return $this->campaignLimit - $this->campaignLimitUsed; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param $reduction |
||
240 | * |
||
241 | * @return $this |
||
242 | * |
||
243 | * @throws \Exception |
||
244 | */ |
||
245 | public function reduceCampaignLimitRemaining($reduction) |
||
246 | { |
||
247 | if (!$this->hasCampaignLimit()) { |
||
248 | throw new \Exception('Campaign Limit was not set'); |
||
249 | } elseif ($this->campaignLimit <= ($this->campaignLimitUsed + $reduction)) { |
||
250 | throw new \Exception('Campaign Limit exceeded'); |
||
251 | } |
||
252 | $this->campaignLimitUsed += $reduction; |
||
253 | |||
254 | return $this; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function resetCampaignLimitRemaining() |
||
261 | { |
||
262 | $this->campaignLimitUsed = 0; |
||
263 | |||
264 | return $this; |
||
265 | } |
||
266 | } |
||
267 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: