Completed
Pull Request — master (#63)
by Jeroen De
02:24
created

Donation::modifyDataObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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