Completed
Pull Request — master (#509)
by
unknown
04:23
created

setPaymentFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\DataAccess;
6
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\ORMException;
9
use WMDE\Fundraising\Entities\MembershipApplication as DoctrineApplication;
10
use WMDE\Fundraising\Frontend\Domain\Model\BankData;
11
use WMDE\Fundraising\Frontend\Domain\Model\EmailAddress;
12
use WMDE\Fundraising\Frontend\Domain\Model\Euro;
13
use WMDE\Fundraising\Frontend\Domain\Model\Iban;
14
use WMDE\Fundraising\Frontend\Domain\Model\MembershipApplicant;
15
use WMDE\Fundraising\Frontend\Domain\Model\MembershipApplication;
16
use WMDE\Fundraising\Frontend\Domain\Model\MembershipPayment;
17
use WMDE\Fundraising\Frontend\Domain\Model\PersonName;
18
use WMDE\Fundraising\Frontend\Domain\Model\PhoneNumber;
19
use WMDE\Fundraising\Frontend\Domain\Model\PhysicalAddress;
20
use WMDE\Fundraising\Frontend\Domain\Repositories\GetMembershipApplicationException;
21
use WMDE\Fundraising\Frontend\Domain\Repositories\MembershipApplicationRepository;
22
use WMDE\Fundraising\Frontend\Domain\Repositories\StoreMembershipApplicationException;
23
24
/**
25
 * @license GNU GPL v2+
26
 * @author Jeroen De Dauw < [email protected] >
27
 */
28
class DoctrineMembershipApplicationRepository implements MembershipApplicationRepository {
29
30
	private $entityManager;
31
32
	public function __construct( EntityManager $entityManager ) {
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
33
		$this->entityManager = $entityManager;
34
	}
35
36
	public function storeApplication( MembershipApplication $application ) {
37
		if ( $application->hasId() ) {
38
			$this->updateApplication( $application );
39
		}
40
		else {
41
			$this->insertApplication( $application );
42
		}
43
	}
44
45
	private function insertApplication( MembershipApplication $application ) {
46
		$doctrineApplication = new DoctrineApplication();
47
		$this->updateDoctrineApplication( $doctrineApplication, $application );
48
49
		try {
50
			$this->entityManager->persist( $doctrineApplication );
51
			$this->entityManager->flush();
52
		}
53
		catch ( ORMException $ex ) {
54
			throw new StoreMembershipApplicationException( $ex );
55
		}
56
57
		$application->assignId( $doctrineApplication->getId() );
58
	}
59
60
	private function updateApplication( MembershipApplication $application ) {
61
		$doctrineApplication = $this->getDoctrineApplicationById( $application->getId() );
62
63
		if ( $doctrineApplication === null ) {
64
			throw new StoreMembershipApplicationException();
65
		}
66
67
		$this->updateDoctrineApplication( $doctrineApplication, $application );
68
69
		try {
70
			$this->entityManager->persist( $doctrineApplication );
71
			$this->entityManager->flush();
72
		}
73
		catch ( ORMException $ex ) {
74
			throw new StoreMembershipApplicationException( $ex );
75
		}
76
	}
77
78
	private function updateDoctrineApplication( DoctrineApplication $doctrineApplication, MembershipApplication $application ) {
79
		$doctrineApplication->setId( $application->getId() );
80
		$doctrineApplication->setMembershipType( $application->getType() );
81
82
		$this->setApplicantFields( $doctrineApplication, $application->getApplicant() );
83
		$this->setPaymentFields( $doctrineApplication, $application->getPayment() );
84
85
		$doctrineApplication->setStatus( $this->getDoctrineStatus( $application ) );
86
	}
87
88
	private function setApplicantFields( DoctrineApplication $application, MembershipApplicant $applicant ) {
89
		$application->setApplicantFirstName( $applicant->getPersonName()->getFirstName() );
90
		$application->setApplicantLastName( $applicant->getPersonName()->getLastName() );
91
		$application->setApplicantSalutation( $applicant->getPersonName()->getSalutation() );
92
		$application->setApplicantTitle( $applicant->getPersonName()->getTitle() );
93
94
		$application->setApplicantDateOfBirth( $applicant->getDateOfBirth() );
95
96
		$application->setApplicantEmailAddress( $applicant->getEmailAddress()->getFullAddress() );
97
		$application->setApplicantPhoneNumber( $applicant->getPhoneNumber()->__toString() );
98
99
		$address = $applicant->getPhysicalAddress();
100
101
		$application->setCity( $address->getCity() );
102
		$application->setCountry( $address->getCountryCode() );
103
		$application->setPostcode( $address->getPostalCode() );
104
		$application->setAddress( $address->getStreetAddress() );
105
	}
106
107
	private function setPaymentFields( DoctrineApplication $application, MembershipPayment $payment ) {
108
		$application->setPaymentIntervalInMonths( $payment->getIntervalInMonths() );
109
		$application->setPaymentAmount( (int)$payment->getAmount()->getEuroFloat() );
110
111
		$bankData = $payment->getBankData();
112
113
		$application->setPaymentBankAccount( $bankData->getAccount() );
114
		$application->setPaymentBankCode( $bankData->getBankCode() );
115
		$application->setPaymentBankName( $bankData->getBankName() );
116
		$application->setPaymentBic( $bankData->getBic() );
117
		$application->setPaymentIban( $bankData->getIban()->toString() );
118
	}
119
120
	private function getDoctrineStatus( MembershipApplication $application ): int {
121
		$status = DoctrineApplication::STATUS_NEUTRAL;
122
123
		if ( $application->needsModeration() ) {
124
			$status += DoctrineApplication::STATUS_MODERATION;
125
		}
126
127
		if ( $application->isCancelled() ) {
128
			$status += DoctrineApplication::STATUS_CANCELED;
129
		}
130
131
		return $status;
132
	}
133
134
	/**
135
	 * @param int $id
136
	 *
137
	 * @return MembershipApplication|null
138
	 * @throws GetMembershipApplicationException
139
	 */
140
	public function getApplicationById( int $id ) {
141
		try {
142
			$application = $this->getDoctrineApplicationById( $id );
143
		}
144
		catch ( ORMException $ex ) {
145
			throw new GetMembershipApplicationException( $ex );
146
		}
147
148
		if ( $application === null ) {
149
			return null;
150
		}
151
152
		return $this->newApplicationDomainEntity( $application );
153
	}
154
155
	/**
156
	 * @param int $id
157
	 * @return DoctrineApplication|null
158
	 * @throws ORMException
159
	 */
160
	public function getDoctrineApplicationById( int $id ) {
161
		return $this->entityManager->find( DoctrineApplication::class, $id );
162
	}
163
164
	private function newApplicationDomainEntity( DoctrineApplication $application ): MembershipApplication {
165
		return new MembershipApplication(
166
			$application->getId(),
167
			$application->getMembershipType(),
168
			new MembershipApplicant(
169
				$this->newPersonName( $application ),
170
				$this->newAddress( $application ),
171
				new EmailAddress( $application->getApplicantEmailAddress() ),
172
				new PhoneNumber( $application->getApplicantPhoneNumber() ),
173
				$application->getApplicantDateOfBirth()
174
			),
175
			new MembershipPayment(
176
				$application->getPaymentIntervalInMonths(),
177
				Euro::newFromFloat( $application->getPaymentAmount() ),
178
				$this->newBankData( $application )
179
			),
180
			$application->needsModeration(),
181
			$application->isCancelled()
182
		);
183
	}
184
185
	private function newPersonName( DoctrineApplication $application ): PersonName {
186
		$personName = PersonName::newPrivatePersonName();
187
188
		$personName->setFirstName( $application->getApplicantFirstName() );
189
		$personName->setLastName( $application->getApplicantLastName() );
190
		$personName->setSalutation( $application->getApplicantSalutation() );
191
		$personName->setTitle( $application->getApplicantTitle() );
192
193
		return $personName->freeze()->assertNoNullFields();
194
	}
195
196
	private function newAddress( DoctrineApplication $application ): PhysicalAddress {
197
		$address = new PhysicalAddress();
198
199
		$address->setCity( $application->getCity() );
200
		$address->setCountryCode( $application->getCountry() );
201
		$address->setPostalCode( $application->getPostcode() );
202
		$address->setStreetAddress( $application->getAddress() );
203
204
		return $address->freeze()->assertNoNullFields();
205
	}
206
207
	private function newBankData( DoctrineApplication $application ): BankData {
208
		$bankData = new BankData();
209
210
		$bankData->setAccount( $application->getPaymentBankAccount() );
211
		$bankData->setBankCode( $application->getPaymentBankCode() );
212
		$bankData->setBankName( $application->getPaymentBankName() );
213
		$bankData->setBic( $application->getPaymentBic() );
214
		$bankData->setIban( new Iban( $application->getPaymentIban() ) );
215
216
		return $bankData->freeze()->assertNoNullFields();
217
	}
218
219
}
220