Completed
Push — master ( c0df86...3cffd5 )
by
unknown
26s
created

Donation   C

Complexity

Total Complexity 69

Size/Duplication

Total Lines 717
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 21.3%

Importance

Changes 0
Metric Value
wmc 69
lcom 2
cbo 1
dl 0
loc 717
ccs 36
cts 169
cp 0.213
rs 5
c 0
b 0
f 0

55 Methods

Rating   Name   Duplication   Size   Complexity  
A setDonorFullName() 0 5 1
A getDonorFullName() 0 3 1
A setDonorCity() 0 5 1
A getDonorCity() 0 3 1
A setDonorEmail() 0 5 1
A getDonorEmail() 0 3 1
A setDonorOptsIntoNewsletter() 0 5 1
A getDonorOptsIntoNewsletter() 0 3 1
A setDonationReceipt() 0 5 1
A getDonationReceipt() 0 3 1
A setPublicRecord() 0 5 1
A getPublicRecord() 0 3 1
A setAmount() 0 5 1
A getAmount() 0 3 1
A setPaymentIntervalInMonths() 0 5 1
A getPaymentIntervalInMonths() 0 3 1
A setPaymentType() 0 5 1
A getPaymentType() 0 3 1
A setComment() 0 5 1
A getComment() 0 3 1
A setBankTransferCode() 0 5 1
A getBankTransferCode() 0 3 1
A setSource() 0 5 1
A getSource() 0 3 1
A setRemoteAddr() 0 5 1
A getRemoteAddr() 0 3 1
A setHash() 0 5 1
A getHash() 0 3 1
A setIsPublic() 0 5 1
A getIsPublic() 0 3 1
A setCreationTime() 0 5 1
A getCreationTime() 0 3 1
A setDeletionTime() 0 5 1
A getDeletionTime() 0 3 1
A setDtExp() 0 5 1
A getDtExp() 0 3 1
A setStatus() 0 5 1
A getStatus() 0 3 1
A setDtGruen() 0 5 1
A getDtGruen() 0 3 1
A setDtBackup() 0 5 1
A getDtBackup() 0 3 1
A getId() 0 3 1
A setId() 0 3 1
A getUExpiry() 0 3 1
A uTokenIsExpired() 0 3 1
A validateToken() 0 9 1
C getEntryType() 0 22 8
A setData() 0 5 1
A getData() 0 3 1
A getDecodedData() 0 9 3
A encodeAndSetData() 0 3 1
A getDataObject() 0 11 4
A setDataObject() 0 18 3
A modifyDataObject() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Donation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Donation, and based on these observations, apply Extract Interface, too.

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