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

FakeApplicationRepository::throwAnonymizedOnRead()   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
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