Completed
Pull Request — master (#90)
by Jeroen De
03:08
created

getApplicationOrNullById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace WMDE\Fundraising\Store;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\ORMException;
7
use WMDE\Fundraising\Entities\MembershipApplication;
8
9
/**
10
 * @since 2.0
11
 *
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class MembershipApplicationRepository {
16
17
	private $entityManager;
18
19
	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...
20
		$this->entityManager = $entityManager;
21
	}
22
23
	/**
24
	 * @param int $applicationId
25
	 *
26
	 * @return MembershipApplication|null
27
	 * @throws MembershipApplicationRepositoryException
28
	 */
29
	public function getApplicationOrNullById( $applicationId ) {
30
		try {
31
			$application = $this->entityManager->find( MembershipApplication::class, $applicationId );
32
		}
33
		catch ( ORMException $ex ) {
34
			throw new MembershipApplicationRepositoryException( 'Membership application could not be accessed' );
35
		}
36
37
		return $application;
38
	}
39
40
	/**
41
	 * @param int $applicationId
42
	 *
43
	 * @return MembershipApplication
44
	 * @throws MembershipApplicationRepositoryException
45
	 */
46
	public function getApplicationById( $applicationId ) {
47
		$application = $this->getApplicationOrNullById( $applicationId );
48
49
		if ( $application === null ) {
50
			throw new MembershipApplicationRepositoryException( 'Membership application does not exist' );
51
		}
52
53
		return $application;
54
	}
55
56
	/**
57
	 * @param MembershipApplication $application
58
	 *
59
	 * @throws MembershipApplicationRepositoryException
60
	 */
61
	public function persistApplication( MembershipApplication $application ) {
62
		try {
63
			$this->entityManager->persist( $application );
64
		}
65
		catch ( ORMException $ex ) {
66
			throw new MembershipApplicationRepositoryException( 'Failed to persist membership application' );
67
		}
68
	}
69
70
	/**
71
	 * @param int $applicationId
72
	 * @param callable $modificationFunction
73
	 *
74
	 * @throws MembershipApplicationRepositoryException
75
	 */
76
	public function modifyApplication( $applicationId, callable $modificationFunction ) {
77
		$application = $this->getApplicationById( $applicationId );
78
79
		$modificationFunction( $application );
80
81
		$this->persistApplication( $application );
82
	}
83
84
	/**
85
	 * @throws MembershipApplicationRepositoryException
86
	 */
87
	public function flush() {
88
		try {
89
			$this->entityManager->flush();
90
		}
91
		catch ( ORMException $ex ) {
92
			throw new MembershipApplicationRepositoryException( 'Failed to persist membership application' );
93
		}
94
	}
95
96
}
97