Completed
Pull Request — master (#519)
by Jeroen De
04:03
created

DoctrineMembershipApplicationTracker::concatenate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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;
10
use WMDE\Fundraising\Frontend\Infrastructure\MembershipApplicationTracker;
11
use WMDE\Fundraising\Frontend\Infrastructure\MembershipApplicationTrackingException;
12
use WMDE\Fundraising\Frontend\Infrastructure\MembershipApplicationTrackingInfo;
13
14
/**
15
 * @licence GNU GPL v2+
16
 * @author Kai Nissen < [email protected] >
17
 */
18
class DoctrineMembershipApplicationTracker implements MembershipApplicationTracker {
19
20
	private $entityManager;
21
22
	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...
23
		$this->entityManager = $entityManager;
24
	}
25
26
	public function trackApplication( int $applicationId, MembershipApplicationTrackingInfo $trackingInfo ) {
27
		$application = $this->getApplicationById( $applicationId );
28
29
		$data = $application->getDecodedData();
30
		$data['confirmationPageCampaign'] = $trackingInfo->getCampaignCode();
31
		$data['confirmationPage'] = $trackingInfo->getKeyword();
32
		$application->encodeAndSetData( $data );
33
34
		$this->persistApplication( $application );
35
	}
36
37
	private function getApplicationById( int $applicationId ): MembershipApplication {
38
		try {
39
			$application = $this->entityManager->find( MembershipApplication::class, $applicationId );
40
		}
41
		catch ( ORMException $ex ) {
42
			// TODO: might want to log failure here
43
			throw new MembershipApplicationTrackingException( 'Membership application could not be accessed' );
44
		}
45
46
		if ( $application === null ) {
47
			throw new MembershipApplicationTrackingException( 'Membership application does not exist' );
48
		}
49
50
		return $application;
51
	}
52
53
	private function persistApplication( MembershipApplication $application ) {
54
		try {
55
			$this->entityManager->persist( $application );
56
			$this->entityManager->flush();
57
		}
58
		catch ( ORMException $ex ) {
59
			throw new MembershipApplicationTrackingException( 'Failed to persist membership application' );
60
		}
61
	}
62
63
}
64