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