Passed
Pull Request — master (#149)
by
unknown
03:33 queued 01:20
created

Donation::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2
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
		if ($this->addressChange === null) {
229 11
			$this->addressChange = new AddressChange();
230
		}
231 11
	}
232
233
	/**
234
	 * @param string $donorFullName
235
	 *
236
	 * @return self
237
	 */
238
	public function setDonorFullName( $donorFullName ) {
239
		$this->donorFullName = $donorFullName;
240
241
		return $this;
242
	}
243
244
	/**
245
	 * @return string
246
	 */
247
	public function getDonorFullName() {
248
		return $this->donorFullName;
249
	}
250
251
	/**
252
	 * @param string $donorCity
253
	 *
254
	 * @return self
255
	 */
256
	public function setDonorCity( $donorCity ) {
257
		$this->donorCity = $donorCity;
258
259
		return $this;
260
	}
261
262
	/**
263
	 * @return string
264
	 */
265
	public function getDonorCity() {
266
		return $this->donorCity;
267
	}
268
269
	/**
270
	 * @param string $donorEmail
271
	 *
272
	 * @return self
273
	 */
274
	public function setDonorEmail( $donorEmail ) {
275
		$this->donorEmail = $donorEmail;
276
277
		return $this;
278
	}
279
280
	/**
281
	 * @return string
282
	 */
283
	public function getDonorEmail() {
284
		return $this->donorEmail;
285
	}
286
287
	/**
288
	 * @param boolean $donorOptsIntoNewsletter
289
	 *
290
	 * @return self
291
	 */
292
	public function setDonorOptsIntoNewsletter( $donorOptsIntoNewsletter ) {
293
		$this->donorOptsIntoNewsletter = $donorOptsIntoNewsletter;
294
295
		return $this;
296
	}
297
298
	/**
299
	 * @return boolean
300
	 */
301
	public function getDonorOptsIntoNewsletter() {
302
		return $this->donorOptsIntoNewsletter;
303
	}
304
305
	/**
306
	 * Set donation receipt state
307
	 *
308
	 * @param boolean $donationReceipt
309
	 * @return self
310
	 */
311
	public function setDonationReceipt( $donationReceipt ) {
312
		$this->donationReceipt = $donationReceipt;
313
314
		return $this;
315
	}
316
317
	/**
318
	 * Get donation receipt state
319
	 *
320
	 * @return boolean
321
	 */
322
	public function getDonationReceipt() {
323
		return $this->donationReceipt;
324
	}
325
326
	/**
327
	 * Set publicly displayed donation record
328
	 *
329
	 * @param string $publicRecord
330
	 * @return self
331
	 */
332
	public function setPublicRecord( $publicRecord ) {
333
		$this->publicRecord = $publicRecord;
334
335
		return $this;
336
	}
337
338
	/**
339
	 * Get publicly displayed donation record
340
	 *
341
	 * @return string
342
	 */
343
	public function getPublicRecord() {
344
		return $this->publicRecord;
345
	}
346
347
	/**
348
	 * @param string $amount
349
	 * @return self
350
	 */
351
	public function setAmount( $amount ) {
352
		$this->amount = $amount;
353
354
		return $this;
355
	}
356
357
	/**
358
	 * @return string
359
	 */
360
	public function getAmount() {
361
		return $this->amount;
362
	}
363
364
	/**
365
	 * @param integer $paymentIntervalInMonths
366
	 *
367
	 * @return self
368
	 */
369
	public function setPaymentIntervalInMonths( $paymentIntervalInMonths ) {
370
		$this->paymentIntervalInMonths = $paymentIntervalInMonths;
371
372
		return $this;
373
	}
374
375
	/**
376
	 * @return integer
377
	 */
378
	public function getPaymentIntervalInMonths() {
379
		return $this->paymentIntervalInMonths;
380
	}
381
382
	/**
383
	 * Set payment type short code
384
	 *
385
	 * @param string $paymentType
386
	 * @return self
387
	 */
388
	public function setPaymentType( $paymentType ) {
389
		$this->paymentType = $paymentType;
390
391
		return $this;
392
	}
393
394
	/**
395
	 * Get payment type short code
396
	 *
397
	 * @return string
398
	 */
399
	public function getPaymentType() {
400
		return $this->paymentType;
401
	}
402
403
	/**
404
	 * @param string $comment
405
	 * @return self
406
	 */
407
	public function setComment( $comment ) {
408
		$this->comment = $comment;
409
410
		return $this;
411
	}
412
413
	/**
414
	 * @return string
415
	 */
416
	public function getComment() {
417
		return $this->comment;
418
	}
419
420
	/**
421
	 * Set bank transfer reference code
422
	 *
423
	 * @param string $bankTransferCode
424
	 *
425
	 * @return self
426
	 */
427
	public function setBankTransferCode( $bankTransferCode ) {
428
		$this->bankTransferCode = $bankTransferCode;
429
430
		return $this;
431
	}
432
433
	/**
434
	 * Get bank transfer reference code
435
	 *
436
	 * @return string
437
	 */
438
	public function getBankTransferCode() {
439
		return $this->bankTransferCode;
440
	}
441
442
	/**
443
	 * @param string $source
444
	 * @return self
445
	 */
446
	public function setSource( $source ) {
447
		$this->source = $source;
448
449
		return $this;
450
	}
451
452
	/**
453
	 * @return string
454
	 */
455
	public function getSource() {
456
		return $this->source;
457
	}
458
459
	/**
460
	 * @param string $remoteAddr
461
	 * @return self
462
	 */
463
	public function setRemoteAddr( $remoteAddr ) {
464
		$this->remoteAddr = $remoteAddr;
465
466
		return $this;
467
	}
468
469
	/**
470
	 * @return string
471
	 */
472
	public function getRemoteAddr() {
473
		return $this->remoteAddr;
474
	}
475
476
	/**
477
	 * @param string $hash
478
	 * @return self
479
	 */
480
	public function setHash( $hash ) {
481
		$this->hash = $hash;
482
483
		return $this;
484
	}
485
486
	/**
487
	 * @return string
488
	 */
489
	public function getHash() {
490
		return $this->hash;
491
	}
492
493
	/**
494
	 * Sets if the donations comment should be public or private.
495
	 * @param boolean $isPublic
496
	 * @return self
497
	 */
498
	public function setIsPublic( $isPublic ) {
499
		$this->isPublic = $isPublic;
500
501
		return $this;
502
	}
503
504
	/**
505
	 * Gets if the donations comment is public or private.
506
	 * @return boolean
507
	 */
508
	public function getIsPublic() {
509
		return $this->isPublic;
510
	}
511
512
	/**
513
	 * @param \DateTime $creationTime
514
	 *
515
	 * @return self
516
	 */
517
	public function setCreationTime( $creationTime ) {
518
		$this->creationTime = $creationTime;
519
520
		return $this;
521
	}
522
523
	/**
524
	 * @return \DateTime
525
	 */
526
	public function getCreationTime() {
527
		return $this->creationTime;
528
	}
529
530
	/**
531
	 * @param \DateTime|null $deletionTime
532
	 *
533
	 * @return self
534
	 */
535
	public function setDeletionTime( $deletionTime ) {
536
		$this->deletionTime = $deletionTime;
537
538
		return $this;
539
	}
540
541
	/**
542
	 * @return \DateTime|null
543
	 */
544
	public function getDeletionTime() {
545
		return $this->deletionTime;
546
	}
547
548
	/**
549
	 * @param \DateTime $dtExp
550
	 * @return self
551
	 */
552
	public function setDtExp( $dtExp ) {
553
		$this->dtExp = $dtExp;
554
555
		return $this;
556
	}
557
558
	/**
559
	 * @return \DateTime
560
	 */
561
	public function getDtExp() {
562
		return $this->dtExp;
563
	}
564
565
	/**
566
	 * @param string $status
567
	 * @return self
568
	 */
569
	public function setStatus( $status ) {
570
		$this->status = $status;
571
572
		return $this;
573
	}
574
575
	/**
576
	 * @return string
577
	 */
578
	public function getStatus() {
579
		return $this->status;
580
	}
581
582
	/**
583
	 * @param \DateTime $dtGruen
584
	 * @return self
585
	 */
586
	public function setDtGruen( $dtGruen ) {
587
		$this->dtGruen = $dtGruen;
588
589
		return $this;
590
	}
591
592
	/**
593
	 * @return \DateTime
594
	 */
595
	public function getDtGruen() {
596
		return $this->dtGruen;
597
	}
598
599
	/**
600
	 * @param \DateTime $dtBackup
601
	 * @return self
602
	 */
603
	public function setDtBackup( $dtBackup ) {
604
		$this->dtBackup = $dtBackup;
605
606
		return $this;
607
	}
608
609
	/**
610
	 * @return \DateTime
611
	 */
612
	public function getDtBackup() {
613
		return $this->dtBackup;
614
	}
615
616
	/**
617
	 * @return integer|null
618
	 */
619 3
	public function getId() {
620 3
		return $this->id;
621
	}
622
623
	public function getPayment(): ?DonationPayment {
624
		return $this->payment;
625
	}
626
627
	public function setPayment( DonationPayment $payment ) {
628
		$this->payment = $payment;
629
	}
630
631
632
	/**
633
	 * @since 2.0
634
	 *
635
	 * @param integer|null $id
636
	 */
637 2
	public function setId( $id ) {
638 2
		$this->id = $id;
639 2
	}
640
641
	public function getUExpiry() {
642
		return $this->getDecodedData()['uexpiry'];
643
	}
644
645
	public function uTokenIsExpired() {
646
		return time() > strtotime( $this->getUExpiry() );
647
	}
648
649
	public function validateToken( $tokenToCheck, $serverSecret ) {
650
		$checkToken = preg_replace( '/\$.*$/', '', $tokenToCheck );
651
652
		$checkToken = $checkToken . '$' .
653
			sha1( sha1( "$checkToken+$serverSecret" ) . '|' .
654
				sha1( "{$this->id}+$serverSecret" ) . '|' .
655
				sha1( "{$this->creationTime->format( 'Y-m-d H:i:s' )}+$serverSecret" ) );
656
		return $checkToken === $tokenToCheck;
657
	}
658
659
	public function getEntryType( $mode = null ) {
660
		$data = $this->getDecodedData();
661
662
		if ( $mode === null ) {
663
			$mode = $this->publicRecord;
664
			if ( !is_int( $mode ) ) {
665
				return $this->publicRecord;
666
			}
667
		}
668
669
		if ( $mode == 1 || $mode == 2 ) {
670
			$eintrag = $this->donorFullName;
671
		} else {
672
			$eintrag = 'anonym';
673
		}
674
675
		if ( ( $mode == 1 || $mode == 3 ) && !empty( $data['ort'] ) ) {
676
			$eintrag .= ', ' . $data['ort'];
677
		}
678
679
		return $eintrag;
680
	}
681
682
	/**
683
	 * @deprecated since 2.0, use encodeAndSetData or setDataObject instead
684
	 *
685
	 * @param string $data Base 64 encoded, serialized PHP array
686
	 * @return self
687
	 */
688
	public function setData( $data ) {
689
		$this->data = $data;
690
691
		return $this;
692
	}
693
694
	/**
695
	 * @deprecated since 2.0, use @see getDecodedData or @see getDataObject instead
696
	 *
697
	 * @return string Base 64 encoded, serialized PHP array
698
	 */
699
	public function getData() {
700
		return $this->data;
701
	}
702
703
	/**
704
	 * NOTE: if possible, use @see getDataObject instead, as it provides a nicer API.
705
	 *
706
	 * @since 2.0
707
	 * @return array
708
	 */
709 8
	public function getDecodedData() {
710 8
		if ( $this->data === null ) {
711 4
			return [];
712
		}
713
714 6
		$data = unserialize( base64_decode( $this->data ) );
715
716 6
		return is_array( $data ) ? $data : [];
717
	}
718
719
	/**
720
	 * NOTE: if possible, use @see modifyDataObject instead, as it provides a nicer API.
721
	 *
722
	 * @since 2.0
723
	 * @param array $data
724
	 */
725 6
	public function encodeAndSetData( array $data ) {
726 6
		$this->data = base64_encode( serialize( $data ) );
727 6
	}
728
729
	/**
730
	 * WARNING: updates made to the return value will not be reflected in the Donation state.
731
	 * Similarly, updates to the Donation state will not propagate to the returned object.
732
	 * To update the Donation state, explicitly call @see setDataObject.
733
	 *
734
	 * @since 2.0
735
	 * @return DonationData
736
	 */
737 3
	public function getDataObject() {
738 3
		$dataArray = $this->getDecodedData();
739
740 3
		$data = new DonationData();
741
742 3
		$data->setAccessToken( array_key_exists( 'token', $dataArray ) ? $dataArray['token'] : null );
743 3
		$data->setUpdateToken( array_key_exists( 'utoken', $dataArray ) ? $dataArray['utoken'] : null );
744 3
		$data->setUpdateTokenExpiry( array_key_exists( 'uexpiry', $dataArray ) ? $dataArray['uexpiry'] : null );
745
746 3
		return $data;
747
	}
748
749
	/**
750
	 * @since 2.0
751
	 * @param DonationData $data
752
	 */
753 4
	public function setDataObject( DonationData $data ) {
754 4
		$dataArray = array_merge(
755 4
			$this->getDecodedData(),
756
			[
757 4
				'token' => $data->getAccessToken(),
758 4
				'utoken' => $data->getUpdateToken(),
759 4
				'uexpiry' => $data->getUpdateTokenExpiry(),
760
			]
761
		);
762
763 4
		foreach ( [ 'token', 'utoken', 'uexpiry' ] as $keyName ) {
764 4
			if ( is_null( $dataArray[$keyName] ) ) {
765 4
				unset( $dataArray[$keyName] );
766
			}
767
		}
768
769 4
		$this->encodeAndSetData( $dataArray );
770 4
	}
771
772
	/**
773
	 * @since 2.0
774
	 * @param callable $modificationFunction Takes a modifiable DonationData parameter
775
	 */
776 1
	public function modifyDataObject( callable $modificationFunction ) {
777 1
		$dataObject = $this->getDataObject();
778 1
		$modificationFunction( $dataObject );
779 1
		$this->setDataObject( $dataObject );
780 1
	}
781
782
	/**
783
	 * Get AddressChange reference
784
	 *
785
	 * @since 8.0
786
	 *
787
	 * @return AddressChange
788
	 */
789
	public function getAddressChange(): AddressChange {
790
		return $this->addressChange;
791
	}
792
}
793