Completed
Push — master ( 88699b...ae29c5 )
by Gabriel
13s
created

FakeApplicationRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 54
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A throwOnRead() 0 3 1
A throwOnWrite() 0 3 1
A storeApplication() 0 10 3
A getApplicationById() 0 15 4
A throwAnonymizedOnRead() 0 3 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\MembershipContext\Tests\Fixtures;
6
7
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Model\Application;
8
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\ApplicationAnonymizedException;
9
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\ApplicationRepository;
10
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\GetMembershipApplicationException;
11
use WMDE\Fundraising\Frontend\MembershipContext\Domain\Repositories\StoreMembershipApplicationException;
12
13
/**
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class FakeApplicationRepository implements ApplicationRepository {
18
19
	private $calls = 0;
20
	private $applications = [];
21
	private $throwOnRead = false;
22
	private $throwOnWrite = false;
23
	private $throwAnonymizedOnRead = false;
24
25
	public function __construct( Application ...$applications ) {
26
		foreach ( $applications as $application ) {
27
			$this->storeApplication( $application );
28
		}
29
	}
30
31
	public function throwOnRead(): void {
32
		$this->throwOnRead = true;
33
	}
34
35
	public function throwOnWrite(): void {
36
		$this->throwOnWrite = true;
37
	}
38
39
	public function throwAnonymizedOnRead(): void {
40
		$this->throwAnonymizedOnRead = true;
41
	}
42
43
	public function storeApplication( Application $application ): void {
44
		if ( $this->throwOnWrite ) {
45
			throw new StoreMembershipApplicationException();
46
		}
47
48
		if ( $application->getId() === null ) {
49
			$application->assignId( ++$this->calls );
50
		}
51
		$this->applications[$application->getId()] = unserialize( serialize( $application ) );
52
	}
53
54
	public function getApplicationById( int $id ): ?Application {
55
		if ( $this->throwAnonymizedOnRead ) {
56
			throw new ApplicationAnonymizedException();
57
		}
58
59
		if ( $this->throwOnRead ) {
60
			throw new GetMembershipApplicationException();
61
		}
62
63
		if ( array_key_exists( $id, $this->applications ) ) {
64
			return unserialize( serialize( $this->applications[$id] ) );
65
		}
66
67
		return null;
68
	}
69
70
}
71