Completed
Pull Request — master (#149)
by
unknown
05:10
created

Donation::setAddressChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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