1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Classes; |
4
|
|
|
|
5
|
|
|
use Ipag\Classes\Contracts\Emptiable; |
6
|
|
|
use Ipag\Classes\Traits\EmptiableTrait; |
7
|
|
|
|
8
|
|
|
final class Subscription extends BaseResource implements Emptiable |
9
|
|
|
{ |
10
|
|
|
use EmptiableTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $profileId; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $frequency; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $interval; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $start; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $cycle; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var float |
39
|
|
|
*/ |
40
|
|
|
private $amount; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $trial; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $trialCycle; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
private $trialFrequency; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var float |
59
|
|
|
*/ |
60
|
|
|
private $trialAmount; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
public function getFrequency() |
66
|
|
|
{ |
67
|
|
|
return $this->frequency; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
public function getInterval() |
74
|
|
|
{ |
75
|
|
|
return $this->interval; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getStart() |
82
|
|
|
{ |
83
|
|
|
return $this->start; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function getCycle() |
90
|
|
|
{ |
91
|
|
|
return $this->cycle; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return float |
96
|
|
|
*/ |
97
|
|
|
public function getAmount() |
98
|
|
|
{ |
99
|
|
|
return $this->amount; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function isTrial() |
106
|
|
|
{ |
107
|
|
|
return $this->trial; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getTrialCycle() |
114
|
|
|
{ |
115
|
|
|
return $this->trialCycle; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
public function getTrialFrequency() |
122
|
|
|
{ |
123
|
|
|
return $this->trialFrequency; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return float |
128
|
|
|
*/ |
129
|
|
|
public function getTrialAmount() |
130
|
|
|
{ |
131
|
|
|
return $this->trialAmount; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $frequency the frequency |
136
|
|
|
*/ |
137
|
|
|
public function setFrequency($frequency) |
138
|
|
|
{ |
139
|
|
|
if (!$this->isValidFrequency($frequency)) { |
140
|
|
|
throw new \UnexpectedValueException( |
141
|
|
|
'A frequencia não é válida ou não tem entre 1 e 2 caracteres' |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
$this->frequency = $frequency; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets the value of interval. |
151
|
|
|
*/ |
152
|
|
|
public function setInterval($interval) |
153
|
|
|
{ |
154
|
|
|
if (!$this->isValidInterval($interval)) { |
155
|
|
|
throw new \UnexpectedValueException( |
156
|
|
|
'O intervalo (interval) não é válido' |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->interval = $interval; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $start the start |
167
|
|
|
*/ |
168
|
|
|
public function setStart($start) |
169
|
|
|
{ |
170
|
|
|
if (!$this->isValidStartFormatDate($start)) { |
171
|
|
|
throw new \UnexpectedValueException( |
172
|
|
|
'A data de início não é valida ou está em formato incorreto, deve ser informada utilizando o formato dd/mm/aaaa' |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->start = $start; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param int $cycle |
183
|
|
|
*/ |
184
|
|
|
public function setCycle($cycle) |
185
|
|
|
{ |
186
|
|
|
if (!$this->isValidCycle($cycle)) { |
187
|
|
|
throw new \UnexpectedValueException( |
188
|
|
|
'O ciclo deve ser númerico e ter entre 1 e 3 caracteres.' |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
$this->cycle = $cycle; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param float $amount |
198
|
|
|
*/ |
199
|
|
|
public function setAmount($amount) |
200
|
|
|
{ |
201
|
|
|
$this->amount = $this->getNumberUtil()->convertToDouble($amount); |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param bool $trial |
208
|
|
|
*/ |
209
|
|
|
public function setTrial($trial) |
210
|
|
|
{ |
211
|
|
|
$this->trial = (bool) $trial; |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param int $trialCycle |
218
|
|
|
*/ |
219
|
|
|
public function setTrialCycle($trialCycle) |
220
|
|
|
{ |
221
|
|
|
if (!$this->isValidCycle($trialCycle)) { |
222
|
|
|
throw new \UnexpectedValueException( |
223
|
|
|
'O ciclo trial deve ser númerico e ter entre 1 e 3 caracteres.' |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
$this->trialCycle = $trialCycle; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param int $trialFrequency |
233
|
|
|
*/ |
234
|
|
|
public function setTrialFrequency($trialFrequency) |
235
|
|
|
{ |
236
|
|
|
if (!$this->isValidFrequency($trialFrequency)) { |
237
|
|
|
throw new \UnexpectedValueException( |
238
|
|
|
'A frequencia trial não é válida ou não tem entre 1 e 2 caracteres' |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
$this->trialFrequency = $trialFrequency; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param float $trialAmount |
248
|
|
|
*/ |
249
|
|
|
public function setTrialAmount($trialAmount) |
250
|
|
|
{ |
251
|
|
|
$this->trialAmount = $this->getNumberUtil()->convertToDouble($trialAmount); |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function getProfileId() |
260
|
|
|
{ |
261
|
|
|
return $this->profileId; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param string $profileId |
266
|
|
|
*/ |
267
|
|
|
public function setProfileId($profileId) |
268
|
|
|
{ |
269
|
|
|
if (!$this->isValidProfileId($profileId)) { |
270
|
|
|
throw new \UnexpectedValueException( |
271
|
|
|
'Profile ID deve ser somente númerico e ter no máximo 32 caracteres.' |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
$this->profileId = $profileId; |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
private function isValidFrequency($frequency) |
280
|
|
|
{ |
281
|
|
|
return (bool) (is_numeric($frequency) && strlen($frequency) >= 1 && strlen($frequency) <= 2); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
private function isValidInterval($interval) |
285
|
|
|
{ |
286
|
|
|
switch ($interval) { |
287
|
|
|
case Enum\Interval::DAY: |
288
|
|
|
case Enum\Interval::WEEK: |
289
|
|
|
case Enum\Interval::MONTH: |
290
|
|
|
return true; |
291
|
|
|
default: |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return false; |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
View Code Duplication |
private function isValidStartFormatDate($date) |
|
|
|
|
299
|
|
|
{ |
300
|
|
|
/* dd/mm/yyyy */ |
301
|
|
|
if (preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $date, $matches)) { |
302
|
|
|
if (checkdate($matches[2], $matches[1], $matches[3])) { |
303
|
|
|
return true; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
private function isValidCycle($cycle) |
311
|
|
|
{ |
312
|
|
|
return (bool) (is_numeric($cycle) && strlen($cycle) >= 1 && strlen($cycle) <= 3); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
private function isValidProfileId($profileId) |
316
|
|
|
{ |
317
|
|
|
return (bool) (is_numeric($profileId) && strlen($profileId) <= 32); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.