|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\DonatingContext\DataAccess; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Doctrine\ORM\ORMException; |
|
9
|
|
|
use WMDE\Fundraising\Entities\Donation as DoctrineDonation; |
|
10
|
|
|
use WMDE\Fundraising\Frontend\DonatingContext\Infrastructure\DonationEventLogException; |
|
11
|
|
|
use WMDE\Fundraising\Frontend\DonatingContext\Infrastructure\DonationEventLogger; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @license GNU GPL v2+ |
|
15
|
|
|
* @author Gabriel Birke < [email protected] > |
|
16
|
|
|
*/ |
|
17
|
|
|
class DoctrineDonationEventLogger implements DonationEventLogger { |
|
18
|
|
|
|
|
19
|
|
|
private $entityManager; |
|
20
|
|
|
private $timestampFunction; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( EntityManager $entityManager, callable $timestampFunction = null ) { |
|
|
|
|
|
|
23
|
|
|
$this->entityManager = $entityManager; |
|
24
|
|
|
if ( is_null( $timestampFunction ) ) { |
|
25
|
|
|
$this->timestampFunction = function () { |
|
26
|
|
|
return date( 'Y-m-d H:i:s' ); |
|
27
|
|
|
}; |
|
28
|
|
|
} else { |
|
29
|
|
|
$this->timestampFunction = $timestampFunction; |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function log( int $donationId, string $message ) { |
|
34
|
|
|
try { |
|
35
|
|
|
/** @var DoctrineDonation $donation */ |
|
36
|
|
|
$donation = $this->entityManager->find( DoctrineDonation::class, $donationId ); |
|
37
|
|
|
} |
|
38
|
|
|
catch ( ORMException $e ) { |
|
39
|
|
|
throw new DonationEventLogException( 'Could not get donation', $e ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ( is_null( $donation ) ) { |
|
43
|
|
|
throw new DonationEventLogException( 'Could not find donation with id ' . $donationId ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$data = $donation->getDecodedData(); |
|
47
|
|
|
if ( empty( $data['log'] ) ) { |
|
48
|
|
|
$data['log'] = []; |
|
49
|
|
|
} |
|
50
|
|
|
$data['log'][call_user_func( $this->timestampFunction )] = $message; |
|
51
|
|
|
$donation->encodeAndSetData( $data ); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
$this->entityManager->persist( $donation ); |
|
55
|
|
|
$this->entityManager->flush(); |
|
56
|
|
|
} |
|
57
|
|
|
catch ( ORMException $e ) { |
|
58
|
|
|
throw new DonationEventLogException( 'Could not store donation', $e ); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
} |
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.