|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Component\Promotion\Model; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Doctrine\Common\Collections\Collection; |
|
18
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
|
19
|
|
|
|
|
20
|
|
|
class Promotion implements PromotionInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use TimestampableTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $id; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $code; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $name; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $description; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* When exclusive, promotion with top priority will be applied |
|
46
|
|
|
* |
|
47
|
|
|
* @var int |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $priority = 0; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Cannot be applied together with other promotions |
|
53
|
|
|
* |
|
54
|
|
|
* @var bool |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $exclusive = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var int |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $usageLimit; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var int |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $used = 0; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var \DateTimeInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $startsAt; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var \DateTimeInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $endsAt; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @var bool |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $couponBased = false; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var Collection|PromotionCouponInterface[] |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $coupons; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @var Collection|PromotionRuleInterface[] |
|
90
|
|
|
*/ |
|
91
|
|
|
protected $rules; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @var Collection|PromotionActionInterface[] |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $actions; |
|
97
|
|
|
|
|
98
|
|
|
public function __construct() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->createdAt = new \DateTime(); |
|
101
|
|
|
|
|
102
|
|
|
$this->coupons = new ArrayCollection(); |
|
|
|
|
|
|
103
|
|
|
$this->rules = new ArrayCollection(); |
|
|
|
|
|
|
104
|
|
|
$this->actions = new ArrayCollection(); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getId() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->id; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getCode(): ?string |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->code; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setCode(?string $code): void |
|
127
|
|
|
{ |
|
128
|
|
|
$this->code = $code; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getName(): ?string |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->name; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setName(?string $name): void |
|
143
|
|
|
{ |
|
144
|
|
|
$this->name = $name; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getDescription(): ?string |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->description; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setDescription(?string $description): void |
|
159
|
|
|
{ |
|
160
|
|
|
$this->description = $description; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getPriority(): int |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->priority; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setPriority(?int $priority): void |
|
175
|
|
|
{ |
|
176
|
|
|
$this->priority = $priority ?? -1; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
|
|
public function isExclusive(): bool |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->exclusive; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setExclusive(?bool $exclusive): void |
|
191
|
|
|
{ |
|
192
|
|
|
$this->exclusive = $exclusive; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* {@inheritdoc} |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getUsageLimit(): ?int |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->usageLimit; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function setUsageLimit(?int $usageLimit): void |
|
207
|
|
|
{ |
|
208
|
|
|
$this->usageLimit = $usageLimit; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* {@inheritdoc} |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getUsed(): int |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->used; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* {@inheritdoc} |
|
221
|
|
|
*/ |
|
222
|
|
|
public function setUsed(?int $used): void |
|
223
|
|
|
{ |
|
224
|
|
|
$this->used = $used; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function incrementUsed(): void |
|
228
|
|
|
{ |
|
229
|
|
|
++$this->used; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function decrementUsed(): void |
|
233
|
|
|
{ |
|
234
|
|
|
--$this->used; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* {@inheritdoc} |
|
239
|
|
|
*/ |
|
240
|
|
|
public function getStartsAt(): ?\DateTimeInterface |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->startsAt; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* {@inheritdoc} |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setStartsAt(?\DateTimeInterface $startsAt): void |
|
249
|
|
|
{ |
|
250
|
|
|
$this->startsAt = $startsAt; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* {@inheritdoc} |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getEndsAt(): ?\DateTimeInterface |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->endsAt; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* {@inheritdoc} |
|
263
|
|
|
*/ |
|
264
|
|
|
public function setEndsAt(?\DateTimeInterface $endsAt): void |
|
265
|
|
|
{ |
|
266
|
|
|
$this->endsAt = $endsAt; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* {@inheritdoc} |
|
271
|
|
|
*/ |
|
272
|
|
|
public function isCouponBased(): bool |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->couponBased; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* {@inheritdoc} |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setCouponBased(?bool $couponBased): void |
|
281
|
|
|
{ |
|
282
|
|
|
$this->couponBased = (bool) $couponBased; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* {@inheritdoc} |
|
287
|
|
|
*/ |
|
288
|
|
|
public function getCoupons(): Collection |
|
289
|
|
|
{ |
|
290
|
|
|
return $this->coupons; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* {@inheritdoc} |
|
295
|
|
|
*/ |
|
296
|
|
|
public function hasCoupons(): bool |
|
297
|
|
|
{ |
|
298
|
|
|
return !$this->coupons->isEmpty(); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* {@inheritdoc} |
|
303
|
|
|
*/ |
|
304
|
|
|
public function hasCoupon(PromotionCouponInterface $coupon): bool |
|
305
|
|
|
{ |
|
306
|
|
|
return $this->coupons->contains($coupon); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* {@inheritdoc} |
|
311
|
|
|
*/ |
|
312
|
|
|
public function addCoupon(PromotionCouponInterface $coupon): void |
|
313
|
|
|
{ |
|
314
|
|
|
if (!$this->hasCoupon($coupon)) { |
|
315
|
|
|
$coupon->setPromotion($this); |
|
316
|
|
|
$this->coupons->add($coupon); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* {@inheritdoc} |
|
322
|
|
|
*/ |
|
323
|
|
|
public function removeCoupon(PromotionCouponInterface $coupon): void |
|
324
|
|
|
{ |
|
325
|
|
|
$coupon->setPromotion(null); |
|
326
|
|
|
$this->coupons->removeElement($coupon); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* {@inheritdoc} |
|
331
|
|
|
*/ |
|
332
|
|
|
public function getRules(): Collection |
|
333
|
|
|
{ |
|
334
|
|
|
return $this->rules; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* {@inheritdoc} |
|
339
|
|
|
*/ |
|
340
|
|
|
public function hasRules(): bool |
|
341
|
|
|
{ |
|
342
|
|
|
return !$this->rules->isEmpty(); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* {@inheritdoc} |
|
347
|
|
|
*/ |
|
348
|
|
|
public function hasRule(PromotionRuleInterface $rule): bool |
|
349
|
|
|
{ |
|
350
|
|
|
return $this->rules->contains($rule); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* {@inheritdoc} |
|
355
|
|
|
*/ |
|
356
|
|
|
public function addRule(PromotionRuleInterface $rule): void |
|
357
|
|
|
{ |
|
358
|
|
|
if (!$this->hasRule($rule)) { |
|
359
|
|
|
$rule->setPromotion($this); |
|
360
|
|
|
$this->rules->add($rule); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* {@inheritdoc} |
|
366
|
|
|
*/ |
|
367
|
|
|
public function removeRule(PromotionRuleInterface $rule): void |
|
368
|
|
|
{ |
|
369
|
|
|
$rule->setPromotion(null); |
|
370
|
|
|
$this->rules->removeElement($rule); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* {@inheritdoc} |
|
375
|
|
|
*/ |
|
376
|
|
|
public function getActions(): Collection |
|
377
|
|
|
{ |
|
378
|
|
|
return $this->actions; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* {@inheritdoc} |
|
383
|
|
|
*/ |
|
384
|
|
|
public function hasActions(): bool |
|
385
|
|
|
{ |
|
386
|
|
|
return !$this->actions->isEmpty(); |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* {@inheritdoc} |
|
391
|
|
|
*/ |
|
392
|
|
|
public function hasAction(PromotionActionInterface $action): bool |
|
393
|
|
|
{ |
|
394
|
|
|
return $this->actions->contains($action); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* {@inheritdoc} |
|
399
|
|
|
*/ |
|
400
|
|
|
public function addAction(PromotionActionInterface $action): void |
|
401
|
|
|
{ |
|
402
|
|
|
if (!$this->hasAction($action)) { |
|
403
|
|
|
$action->setPromotion($this); |
|
404
|
|
|
$this->actions->add($action); |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* {@inheritdoc} |
|
410
|
|
|
*/ |
|
411
|
|
|
public function removeAction(PromotionActionInterface $action): void |
|
412
|
|
|
{ |
|
413
|
|
|
$action->setPromotion(null); |
|
414
|
|
|
$this->actions->removeElement($action); |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..