Skip to content

Commit

Permalink
refactor: replace db with meta in donor repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Jan 16, 2025
1 parent 10b1748 commit 7e0326b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
24 changes: 5 additions & 19 deletions src/Donors/Repositories/DonorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function getAdditionalEmails(int $donorId)
}

/**
* @unreleased store meta using native WP functions
* @since 3.7.0 Add support to "phone" property
* @since 2.24.0 add support for $donor->totalAmountDonated and $donor->totalNumberOfDonation
* @since 2.21.0 add actions givewp_donor_creating and givewp_donor_created
Expand Down Expand Up @@ -142,22 +143,12 @@ public function insert(Donor $donor)
$donorId = DB::last_insert_id();

foreach ($this->getCoreDonorMeta($donor) as $metaKey => $metaValue) {
DB::table('give_donormeta')
->insert([
'donor_id' => $donorId,
'meta_key' => $metaKey,
'meta_value' => $metaValue,
]);
give()->donor_meta->add_meta($donorId, $metaKey, $metaValue);
}

if (isset($donor->additionalEmails)) {
foreach ($donor->additionalEmails as $additionalEmail) {
DB::table('give_donormeta')
->insert([
'donor_id' => $donorId,
'meta_key' => DonorMetaKeys::ADDITIONAL_EMAILS,
'meta_value' => $additionalEmail,
]);
give()->donor_meta->add_meta($donorId, DonorMetaKeys::ADDITIONAL_EMAILS, $additionalEmail);
}
}
} catch (Exception $exception) {
Expand Down Expand Up @@ -411,6 +402,7 @@ public function prepareQuery(): DonorModelQueryBuilder
* Additional emails are assigned to the same additional_email meta key.
* In order to update them we need to delete and re-insert.
*
* @unreleased store meta using native WP functions
* @since 2.19.6
*
* @return void
Expand All @@ -426,13 +418,7 @@ private function updateAdditionalEmails(Donor $donor)
}

foreach ($donor->additionalEmails as $additionalEmail) {
DB::table('give_donormeta')
->where('donor_id', $donor->id)
->insert([
'donor_id' => $donor->id,
'meta_key' => DonorMetaKeys::ADDITIONAL_EMAILS,
'meta_value' => $additionalEmail,
]);
give()->donor_meta->add_meta($donor->id, DonorMetaKeys::ADDITIONAL_EMAILS, $additionalEmail);
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/Unit/Donors/Repositories/TestDonorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Give\Donors\Models\Donor;
use Give\Donors\Repositories\DonorRepository;
use Give\Donors\ValueObjects\DonorMetaKeys;
use Give\Framework\Database\DB;
use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
use Give\Subscriptions\Repositories\SubscriptionRepository;
Expand Down Expand Up @@ -211,4 +212,32 @@ public function testDeleteShouldRemoveDonorFromTheDatabase()
$this->assertNull($donorQuery);
$this->assertEmpty($donorMetaQuery);
}

/**
* @unreleased
* @throws \Give\Framework\Exceptions\Primitives\Exception|Exception
*/
public function testInsertShouldSafelyStoreMetaValues(): void
{
$name = (object)['first' => 'Jon', 'last' => 'Doe'];

$serializedFirstName = serialize($name);

$donor = new Donor(array_merge(Donor::factory()->definition(), [
'firstName' => $serializedFirstName,
]));

$repository = new DonorRepository();

$repository->insert($donor);

$metaValue = give()->donor_meta->get_meta($donor->id, DonorMetaKeys::FIRST_NAME, true);
$metaQuery = DB::table('give_donormeta')
->where('donor_id', $donor->id)
->where('meta_key', DonorMetaKeys::FIRST_NAME)
->get();

$this->assertSame($serializedFirstName, $metaValue);
$this->assertSame(serialize($serializedFirstName), $metaQuery->meta_value);
}
}

0 comments on commit 7e0326b

Please sign in to comment.