Completed
Pull Request — master (#149)
by
unknown
03:33
created

Donation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Entities;
6
7
use Doctrine\ORM\Mapping as ORM;
8
use Gedmo\Mapping\Annotation as Gedmo;
9
use WMDE\Fundraising\Store\DonationData;
10
11
/**
12
 * @since 2.0
13
 *
14
 * @ORM\Table(
15
 *     name="spenden",
16
 *     indexes={
17
 *         @ORM\Index(name="d_email", columns={"email"}, flags={"fulltext"}),
18
 *         @ORM\Index(name="d_name", columns={"name"}, flags={"fulltext"}),
19
 *         @ORM\Index(name="d_ort", columns={"ort"}, flags={"fulltext"}),
20
 *         @ORM\Index(name="d_dt_new", columns={"dt_new", "is_public"}),
21
 *         @ORM\Index(name="d_zahlweise", columns={"zahlweise", "dt_new"}),
22
 *         @ORM\Index(name="d_dt_gruen", columns={"dt_gruen", "dt_del"}),
23
 *         @ORM\Index(name="d_ueb_code", columns={"ueb_code"}),
24
 *         @ORM\Index(name="d_dt_backup", columns={"dt_backup"}),
25
 *         @ORM\Index(name="d_status", columns={"status", "dt_new"}),
26
 *         @ORM\Index(name="d_comment_list", columns={"is_public", "dt_del"})
27
 *     }
28
 * )
29
 * @ORM\Entity
30
 */
31
class Donation {
32
33
	/**
34
	 * @since 2.0
35
	 */
36
	public const STATUS_NEW = 'N'; // status for direct debit
37
	public const STATUS_PROMISE = 'Z'; // status for bank transfer
38
	public const STATUS_EXTERNAL_INCOMPLETE = 'X'; // status for external payments
39
	public const STATUS_EXTERNAL_BOOKED = 'B'; // status for external payments
40
	public const STATUS_MODERATION = 'P';
41
	public const STATUS_CANCELLED = 'D';
42
43
	/**
44
	 * @since 6.1
45
	 * @deprecated since 6.1; This status is defined for historical reasons. It should not be used to define a
46
	 * donation's status anymore.
47
	 */
48
	public const STATUS_EXPORTED = 'E';
49
50
	/**
51
	 * @var integer
52
	 *
53
	 * @ORM\Column(name="id", type="integer")
54
	 * @ORM\Id
55
	 * @ORM\GeneratedValue(strategy="IDENTITY")
56
	 */
57
	private $id;
58
59
	/**
60
	 * @var string
61
	 *
62
	 * @ORM\Column(name="status", type="string", length=1, options={"default":"N", "fixed":true}, nullable=false)
63
	 */
64
	private $status = self::STATUS_NEW;
65
66
	/**
67
	 * @var string
68
	 *
69
	 * @ORM\Column(name="name", type="string", length=250, nullable=true)
70
	 */
71
	private $donorFullName;
72
73
	/**
74
	 * @var string
75
	 *
76
	 * @ORM\Column(name="ort", type="string", length=250, nullable=true)
77
	 */
78
	private $donorCity;
79
80
	/**
81
	 * @var string
82
	 *
83
	 * @ORM\Column(name="email", type="string", length=250, nullable=true)
84
	 */
85
	private $donorEmail;
86
87
	/**
88
	 * @var boolean
89
	 *
90
	 * @ORM\Column(name="info", type="boolean", options={"default":0}, nullable=false)
91
	 */
92
	private $donorOptsIntoNewsletter = 0;
93
94
	/**
95
	 * @var boolean
96
	 *
97
	 * @ORM\Column(name="bescheinigung", type="boolean", nullable=true)
98
	 */
99
	private $donationReceipt;
100
101
	/**
102
	 * @var string
103
	 *
104
	 * @ORM\Column(name="eintrag", type="string", length=250, options={"default":""}, nullable=false)
105
	 */
106
	private $publicRecord = '';
107
108
	/**
109
	 * @var string
110
	 *
111
	 * @ORM\Column(name="betrag", type="string", length=250, nullable=true)
112
	 */
113
	private $amount;
114
115
	/**
116
	 * @var integer
117
	 *
118
	 * @ORM\Column(name="periode", type="smallint", options={"default":0}, nullable=false)
119
	 */
120
	private $paymentIntervalInMonths = 0;
121
122
	/**
123
	 * @var string
124
	 *
125
	 * @ORM\Column(name="zahlweise", type="string", length=3, options={"default":"BEZ", "fixed":true}, nullable=false)
126
	 */
127
	private $paymentType = 'BEZ';
128
129
	/**
130
	 * @var string
131
	 *
132
	 * @ORM\Column(name="kommentar", type="text", options={"default":""}, nullable=false)
133
	 */
134
	private $comment = '';
135
136
	/**
137
	 * @var string
138
	 *
139
	 * @ORM\Column(name="ueb_code", type="string", length=32, options={"default":""}, nullable=false)
140
	 */
141
	private $bankTransferCode = '';
142
143
	/**
144
	 * @var string
145
	 *
146
	 * @ORM\Column(name="data", type="text", nullable=true)
147
	 */
148
	private $data;
149
150
	/**
151
	 * @var string
152
	 *
153
	 * @ORM\Column(name="source", type="string", length=250, nullable=true)
154
	 */
155
	private $source;
156
157
	/**
158
	 * @var string
159
	 *
160
	 * @ORM\Column(name="remote_addr", type="string", length=250, options={"default":""}, nullable=false)
161
	 */
162
	private $remoteAddr = '';
163
164
	/**
165
	 * @var string
166
	 *
167
	 * @ORM\Column(name="hash", type="string", length=250, nullable=true)
168
	 */
169
	private $hash;
170
171
	/**
172
	 * @var boolean
173
	 *
174
	 * @ORM\Column(name="is_public", type="boolean", options={"default":0}, nullable=false)
175
	 */
176
	private $isPublic = 0;
177
178
	/**
179
	 * @var \DateTime
180
	 *
181
	 * @Gedmo\Timestampable(on="create")
182
	 * @ORM\Column(name="dt_new", type="datetime")
183
	 */
184
	private $creationTime;
185
186
	/**
187
	 * @var \DateTime
188
	 *
189
	 * @ORM\Column(name="dt_del", type="datetime", nullable=true)
190
	 */
191
	private $deletionTime;
192
193
	/**
194
	 * @var \DateTime
195
	 *
196
	 * @ORM\Column(name="dt_exp", type="datetime", nullable=true)
197
	 */
198
	private $dtExp;
199
200
	/**
201
	 * @var \DateTime
202
	 *
203
	 * @ORM\Column(name="dt_gruen", type="datetime", nullable=true)
204
	 */
205
	private $dtGruen;
206
207
	/**
208
	 * @var \DateTime
209
	 *
210
	 * @ORM\Column(name="dt_backup", type="datetime", nullable=true)
211
	 */
212
	private $dtBackup;
213
214
	/**
215
	 * @ORM\OneToOne(targetEntity="WMDE\Fundraising\Entities\DonationPayment", cascade={"all"}, fetch="EAGER")
216
	 */
217
	private $payment;
218
219
	/**
220
	 * @ORM\OneToOne(targetEntity="WMDE\Fundraising\Entities\AddressChange", cascade={"all"}, fetch="EAGER")
221
	 */
222
	private $addressChange;
223
224
	/**
225
	 * Donation constructor creates a new AddressChange entity unless one is supplied by Doctrine
226
	 */
227 11
	public function __construct() {
228 11
		$this->addressChange = new AddressChange();
229 11
	}
230
231
	/**
232
	 * @param string $donorFullName
233
	 *
234
	 * @return self
235
	 */
236
	public function setDonorFullName( $donorFullName ) {
237
		$this->donorFullName = $donorFullName;
238
239
		return $this;
240
	}
241
242
	/**
243
	 * @return string
244
	 */
245
	public function getDonorFullName() {
246
		return $this->donorFullName;
247
	}
248
249
	/**
250
	 * @param string $donorCity
251
	 *
252
	 * @return self
253
	 */
254
	public function setDonorCity( $donorCity ) {
255
		$this->donorCity = $donorCity;
256
257
		return $this;
258
	}
259
260
	/**
261
	 * @return string
262
	 */
263
	public function getDonorCity() {
264
		return $this->donorCity;
265
	}
266
267
	/**
268
	 * @param string $donorEmail
269
	 *
270
	 * @return self
271
	 */
272
	public function setDonorEmail( $donorEmail ) {
273
		$this->donorEmail = $donorEmail;
274
275
		return $this;
276
	}
277
278
	/**
279
	 * @return string
280
	 */
281
	public function getDonorEmail() {
282
		return $this->donorEmail;
283
	}
284
285
	/**
286
	 * @param boolean $donorOptsIntoNewsletter
287
	 *
288
	 * @return self
289
	 */
290
	public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) {
291
		$this->donorOptsIntoNewsletter = $donorOptsIntoNewsletter;
292
293
		return $this;
294
	}
295
296
	/**
297
	 * @return boolean
298
	 */
299
	public function getDonorOptsIntoNewsletter() {
300
		return $this->donorOptsIntoNewsletter;
301
	}
302
303
	/**
304
	 * Set donation receipt state
305
	 *
306
	 * @param boolean $donationReceipt
307
	 * @return self
308
	 */
309
	public function setDonationReceipt( $donationReceipt ) {
310
		$this->donationReceipt = $donationReceipt;
311
312
		return $this;
313
	}
314
315
	/**
316
	 * Get donation receipt state
317
	 *
318
	 * @return boolean
319
	 */
320
	public function getDonationReceipt() {
321
		return $this->donationReceipt;
322
	}
323
324
	/**
325
	 * Set publicly displayed donation record
326
	 *
327
	 * @param string $publicRecord
328
	 * @return self
329
	 */
330
	public function setPublicRecord( $publicRecord ) {
331
		$this->publicRecord = $publicRecord;
332
333
		return $this;
334
	}
335
336
	/**
337
	 * Get publicly displayed donation record
338
	 *
339
	 * @return string
340
	 */
341
	public function getPublicRecord() {
342
		return $this->publicRecord;
343
	}
344
345
	/**
346
	 * @param string $amount
347
	 * @return self
348
	 */
349
	public function setAmount( $amount ) {
350
		$this->amount = $amount;
351
352
		return $this;
353
	}
354
355
	/**
356
	 * @return string
357
	 */
358
	public function getAmount() {
359
		return $this->amount;
360
	}
361
362
	/**
363
	 * @param integer $paymentIntervalInMonths
364
	 *
365
	 * @return self
366
	 */
367
	public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) {
368
		$this->paymentIntervalInMonths = $paymentIntervalInMonths;
369
370
		return $this;
371
	}
372
373
	/**
374
	 * @return integer
375
	 */
376
	public function getPaymentIntervalInMonths() {
377
		return $this->paymentIntervalInMonths;
378
	}
379
380
	/**
381
	 * Set payment type short code
382
	 *
383
	 * @param string $paymentType
384
	 * @return self
385
	 */
386
	public function setPaymentType( $paymentType ) {
387
		$this->paymentType = $paymentType;
388
389
		return $this;
390
	}
391
392
	/**
393
	 * Get payment type short code
394
	 *
395
	 * @return string
396
	 */
397
	public function getPaymentType() {
398
		return $this->paymentType;
399
	}
400
401
	/**
402
	 * @param string $comment
403
	 * @return self
404
	 */
405
	public function setComment( $comment ) {
406
		$this->comment = $comment;
407
408
		return $this;
409
	}
410
411
	/**
412
	 * @return string
413
	 */
414
	public function getComment() {
415
		return $this->comment;
416
	}
417
418
	/**
419
	 * Set bank transfer reference code
420
	 *
421
	 * @param string $bankTransferCode
422
	 *
423
	 * @return self
424
	 */
425
	public function setBankTransferCode( $bankTransferCode ) {
426
		$this->bankTransferCode = $bankTransferCode;
427
428
		return $this;
429
	}
430
431
	/**
432
	 * Get bank transfer reference code
433
	 *
434
	 * @return string
435
	 */
436
	public function getBankTransferCode() {
437
		return $this->bankTransferCode;
438
	}
439
440
	/**
441
	 * @param string $source
442
	 * @return self
443
	 */
444
	public function setSource( $source ) {
445
		$this->source = $source;
446
447
		return $this;
448
	}
449
450
	/**
451
	 * @return string
452
	 */
453
	public function getSource() {
454
		return $this->source;
455
	}
456
457
	/**
458
	 * @param string $remoteAddr
459
	 * @return self
460
	 */
461
	public function setRemoteAddr( $remoteAddr ) {
462
		$this->remoteAddr = $remoteAddr;
463
464
		return $this;
465
	}
466
467
	/**
468
	 * @return string
469
	 */
470
	public function getRemoteAddr() {
471
		return $this->remoteAddr;
472
	}
473
474
	/**
475
	 * @param string $hash
476
	 * @return self
477
	 */
478
	public function setHash( $hash ) {
479
		$this->hash = $hash;
480
481
		return $this;
482
	}
483
484
	/**
485
	 * @return string
486
	 */
487
	public function getHash() {
488
		return $this->hash;
489
	}
490
491
	/**
492
	 * Sets if the donations comment should be public or private.
493
	 * @param boolean $isPublic
494
	 * @return self
495
	 */
496
	public function setIsPublic( $isPublic ) {
497
		$this->isPublic = $isPublic;
498
499
		return $this;
500
	}
501
502
	/**
503
	 * Gets if the donations comment is public or private.
504
	 * @return boolean
505
	 */
506
	public function getIsPublic() {
507
		return $this->isPublic;
508
	}
509
510
	/**
511
	 * @param \DateTime $creationTime
512
	 *
513
	 * @return self
514
	 */
515
	public function setCreationTime( $creationTime ) {
516
		$this->creationTime = $creationTime;
517
518
		return $this;
519
	}
520
521
	/**
522
	 * @return \DateTime
523
	 */
524
	public function getCreationTime() {
525
		return $this->creationTime;
526
	}
527
528
	/**
529
	 * @param \DateTime|null $deletionTime
530
	 *
531
	 * @return self
532
	 */
533
	public function setDeletionTime( $deletionTime ) {
534
		$this->deletionTime = $deletionTime;
535
536
		return $this;
537
	}
538
539
	/**
540
	 * @return \DateTime|null
541
	 */
542
	public function getDeletionTime() {
543
		return $this->deletionTime;
544
	}
545
546
	/**
547
	 * @param \DateTime $dtExp
548
	 * @return self
549
	 */
550
	public function setDtExp( $dtExp ) {
551
		$this->dtExp = $dtExp;
552
553
		return $this;
554
	}
555
556
	/**
557
	 * @return \DateTime
558
	 */
559
	public function getDtExp() {
560
		return $this->dtExp;
561
	}
562
563
	/**
564
	 * @param string $status
565
	 * @return self
566
	 */
567
	public function setStatus( $status ) {
568
		$this->status = $status;
569
570
		return $this;
571
	}
572
573
	/**
574
	 * @return string
575
	 */
576
	public function getStatus() {
577
		return $this->status;
578
	}
579
580
	/**
581
	 * @param \DateTime $dtGruen
582
	 * @return self
583
	 */
584
	public function setDtGruen( $dtGruen ) {
585
		$this->dtGruen = $dtGruen;
586
587
		return $this;
588
	}
589
590
	/**
591
	 * @return \DateTime
592
	 */
593
	public function getDtGruen() {
594
		return $this->dtGruen;
595
	}
596
597
	/**
598
	 * @param \DateTime $dtBackup
599
	 * @return self
600
	 */
601
	public function setDtBackup( $dtBackup ) {
602
		$this->dtBackup = $dtBackup;
603
604
		return $this;
605
	}
606
607
	/**
608
	 * @return \DateTime
609
	 */
610
	public function getDtBackup() {
611
		return $this->dtBackup;
612
	}
613
614
	/**
615
	 * @return integer|null
616
	 */
617 3
	public function getId() {
618 3
		return $this->id;
619
	}
620
621
	public function getPayment(): ?DonationPayment {
622
		return $this->payment;
623
	}
624
625
	public function setPayment( DonationPayment $payment ) {
626
		$this->payment = $payment;
627
	}
628
629
630
	/**
631
	 * @since 2.0
632
	 *
633
	 * @param integer|null $id
634
	 */
635 2
	public function setId( $id ) {
636 2
		$this->id = $id;
637 2
	}
638
639
	public function getUExpiry() {
640
		return $this->getDecodedData()['uexpiry'];
641
	}
642
643
	public function uTokenIsExpired() {
644
		return time() > strtotime( $this->getUExpiry() );
645
	}
646
647
	public function validateToken( $tokenToCheck, $serverSecret ) {
648
		$checkToken = preg_replace( '/\$.*$/', '', $tokenToCheck );
649
650
		$checkToken = $checkToken . '$' .
651
			sha1( sha1( "$checkToken+$serverSecret" ) . '|' .
652
				sha1( "{$this->id}+$serverSecret" ) . '|' .
653
				sha1( "{$this->creationTime->format( 'Y-m-d H:i:s' )}+$serverSecret" ) );
654
		return $checkToken === $tokenToCheck;
655
	}
656
657
	public function getEntryType( $mode = null ) {
658
		$data = $this->getDecodedData();
659
660
		if ( $mode === null ) {
661
			$mode = $this->publicRecord;
662
			if ( !is_int( $mode ) ) {
663
				return $this->publicRecord;
664
			}
665
		}
666
667
		if ( $mode == 1 || $mode == 2 ) {
668
			$eintrag = $this->donorFullName;
669
		} else {
670
			$eintrag = 'anonym';
671
		}
672
673
		if ( ( $mode == 1 || $mode == 3 ) && !empty( $data['ort'] ) ) {
674
			$eintrag .= ', ' . $data['ort'];
675
		}
676
677
		return $eintrag;
678
	}
679
680
	/**
681
	 * @deprecated since 2.0, use encodeAndSetData or setDataObject instead
682
	 *
683
	 * @param string $data Base 64 encoded, serialized PHP array
684
	 * @return self
685
	 */
686
	public function setData( $data ) {
687
		$this->data = $data;
688
689
		return $this;
690
	}
691
692
	/**
693
	 * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead
694
	 *
695
	 * @return string Base 64 encoded, serialized PHP array
696
	 */
697
	public function getData() {
698
		return $this->data;
699
	}
700
701
	/**
702
	 * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API.
703
	 *
704
	 * @since 2.0
705
	 * @return array
706
	 */
707 8
	public function getDecodedData() {
708 8
		if ( $this->data === null ) {
709 4
			return [];
710
		}
711
712 6
		$data = unserialize( base64_decode( $this->data ) );
713
714 6
		return is_array( $data ) ? $data : [];
715
	}
716
717
	/**
718
	 * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API.
719
	 *
720
	 * @since 2.0
721
	 * @param array $data
722
	 */
723 6
	public function encodeAndSetData( array $data ) {
724 6
		$this->data = base64_encode( serialize( $data ) );
725 6
	}
726
727
	/**
728
	 * WARNING: updates made to the return value will not be reflected in the Donation state.
729
	 * Similarly, updates to the Donation state will not propagate to the returned object.
730
	 * To update the Donation state, explicitly call @see setDataObject.
731
	 *
732
	 * @since 2.0
733
	 * @return DonationData
734
	 */
735 3
	public function getDataObject() {
736 3
		$dataArray = $this->getDecodedData();
737
738 3
		$data = new DonationData();
739
740 3
		$data->setAccessToken( array_key_exists( 'token', $dataArray ) ? $dataArray['token'] : null );
741 3
		$data->setUpdateToken( array_key_exists( 'utoken', $dataArray ) ? $dataArray['utoken'] : null );
742 3
		$data->setUpdateTokenExpiry( array_key_exists( 'uexpiry', $dataArray ) ? $dataArray['uexpiry'] : null );
743
744 3
		return $data;
745
	}
746
747
	/**
748
	 * @since 2.0
749
	 * @param DonationData $data
750
	 */
751 4
	public function setDataObject( DonationData $data ) {
752 4
		$dataArray = array_merge(
753 4
			$this->getDecodedData(),
754
			[
755 4
				'token' => $data->getAccessToken(),
756 4
				'utoken' => $data->getUpdateToken(),
757 4
				'uexpiry' => $data->getUpdateTokenExpiry(),
758
			]
759
		);
760
761 4
		foreach ( [ 'token', 'utoken', 'uexpiry' ] as $keyName ) {
762 4
			if ( is_null( $dataArray[$keyName] ) ) {
763 4
				unset( $dataArray[$keyName] );
764
			}
765
		}
766
767 4
		$this->encodeAndSetData( $dataArray );
768 4
	}
769
770
	/**
771
	 * @since 2.0
772
	 * @param callable $modificationFunction Takes a modifiable DonationData parameter
773
	 */
774 1
	public function modifyDataObject( callable $modificationFunction ) {
775 1
		$dataObject = $this->getDataObject();
776 1
		$modificationFunction( $dataObject );
777 1
		$this->setDataObject( $dataObject );
778 1
	}
779
780
	/**
781
	 * Get AddressChange reference
782
	 *
783
	 * @since 8.0
784
	 *
785
	 * @return AddressChange
786
	 */
787
	public function getAddressChange(): AddressChange {
788
		return $this->addressChange;
789
	}
790
}
791