|
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 ) { |
|
|
|
|
|
|
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
|
|
|
|
The
EntityManagermight 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:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.