Completed
Pull Request — master (#108)
by Jeroen De
03:37
created

DoctrineDonation::getDtBackup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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