|
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 ) { |
|
|
|
|
|
|
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
|
|
|
|
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.