-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDonorAccountRepository.php
More file actions
78 lines (65 loc) · 2.68 KB
/
Copy pathDonorAccountRepository.php
File metadata and controls
78 lines (65 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace MatchBot\Domain;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityRepository;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* @extends EntityRepository<DonorAccount>
*/
class DonorAccountRepository extends EntityRepository
{
/**
* @throws UniqueConstraintViolationException if we already have a donor account with the same Stripe Customer ID.
*/
public function save(DonorAccount $donorAccount, ?LoggerInterface $log = null): void
{
$log?->info('DON-1188: in \MatchBot\Domain\DonorAccountRepository::save');
$this->getEntityManager()->persist($donorAccount);
$log?->info('DON-1188: persisted donor account');
try {
$this->getEntityManager()->flush();
$log?->info('DON-1188: flushed');
} catch (\Throwable $t) {
$log?->error('DON-1188: failed to flush donor account', ['exception' => $t]);
throw $t;
}
}
public function findByStripeIdOrNull(StripeCustomerId $stripeAccountId): ?DonorAccount
{
// see https://github.com/laravel-doctrine/fluent/issues/51 for using findOneBy on a field of an embeddable.
return $this->findOneBy(['stripeCustomerId.stripeCustomerId' => $stripeAccountId->stripeCustomerId]);
}
public function findByPersonId(PersonId $personId): ?DonorAccount
{
return $this->findOneBy(['uuid' => $personId->id]);
}
public function findByEmail(EmailAddress $emailAddress): ?DonorAccount
{
return $this->findOneBy(['emailAddress.email' => $emailAddress->email]);
}
/**
* @return bool Whether there is a donor account registered that has the same email address as this donation but
* was not used to make the donation - i.e. so we can invite the donor to log in to it next time as they didn't when
* making this donation.
*/
public function accountExistsMatchingEmailWithDonation(Donation $donation): bool
{
$emailAddress = $donation->getDonorEmailAddress();
if ($emailAddress === null) {
return false;
}
$donorAccountForEmail = $this->findByEmail($emailAddress);
$donorIdFromDonation = $donation->getDonorId();
if ($donorIdFromDonation == null) {
// all donations made since April 2025 have non-null donorID.
return $donorAccountForEmail !== null;
}
return $donorAccountForEmail !== null && !$donorAccountForEmail->id()->equals($donorIdFromDonation);
}
public function delete(DonorAccount $donorAccount): void
{
$this->getEntityManager()->remove($donorAccount);
$this->getEntityManager()->flush();
}
}