Skip to content

Commit

Permalink
Merge pull request #218 from wmde/int-donation-id
Browse files Browse the repository at this point in the history
Remove nullable ID from domain Donation entity
  • Loading branch information
gbirke authored Jul 11, 2023
2 parents 4a38e4e + f6eae1f commit 2f50eaa
Show file tree
Hide file tree
Showing 41 changed files with 695 additions and 650 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<index name="d_payment_id" columns="payment_id"/>
</indexes>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
<generator strategy="NONE"/>
</id>
<field name="status" type="string" column="status" length="1" nullable="false">
<options>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="WMDE\Fundraising\DonationContext\Domain\Model\DonationId" table="last_generated_donation_id">
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="donationId" type="integer" column="donation_id" nullable="false">
<options>
<option name="unsigned">true</option>
<option name="default">0</option>
</options>
</field>
</entity>
</doctrine-mapping>
217 changes: 0 additions & 217 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Repositories\\\\CommentWithAmount\\:\\:setDonationId\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/DataAccess/DoctrineCommentFinder.php

-
message: "#^Method WMDE\\\\Fundraising\\\\DonationContext\\\\DataAccess\\\\DoctrineDonationPrePersistSubscriber\\:\\:prePersist\\(\\) has parameter \\$args with generic class Doctrine\\\\Persistence\\\\Event\\\\LifecycleEventArgs but does not specify its types\\: TObjectManager$#"
count: 1
path: src/DataAccess/DoctrineDonationPrePersistSubscriber.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\DataAccess\\\\DoctrineDonationRepository\\:\\:getDoctrineDonationById\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/DataAccess/DoctrineDonationRepository.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\Donation\\:\\:assignId\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/DataAccess/DoctrineDonationRepository.php

-
message: "#^Cannot call method getAuthorDisplayName\\(\\) on WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\DonationComment\\|null\\.$#"
count: 1
path: src/Domain/Model/Donation.php

-
message: "#^Cannot call method getCommentText\\(\\) on WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\DonationComment\\|null\\.$#"
count: 1
path: src/Domain/Model/Donation.php

-
message: "#^Method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\DonorType\\:\\:__callStatic\\(\\) has parameter \\$arguments with no type specified\\.$#"
count: 1
Expand All @@ -39,195 +14,3 @@ parameters:
message: "#^Method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\DonorType\\:\\:__callStatic\\(\\) has parameter \\$name with no type specified\\.$#"
count: 1
path: src/Domain/Model/DonorType.php

-
message: "#^Method WMDE\\\\Fundraising\\\\DonationContext\\\\Infrastructure\\\\DonationMailer\\:\\:getAdminTemplateArguments\\(\\) should return array\\{id\\: int, moderationFlags\\: array\\<string, bool\\>, amount\\: float\\} but returns array\\{id\\: int\\|null, moderationFlags\\: array\\<string, bool\\>, amount\\: float\\}\\.$#"
count: 1
path: src/Infrastructure/DonationMailer.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationTokenFetcher\\:\\:getTokens\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/Infrastructure/HttpDonationNotifier.php

-
message: "#^Parameter \\#1 \\$donationId of class WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Event\\\\DonationCreatedEvent constructor expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/AddDonation/AddDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationTokenFetcher\\:\\:getTokens\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/AddDonation/AddDonationUseCase.php

-
message: "#^Parameter \\#1 \\$itemId of class WMDE\\\\Fundraising\\\\PaymentContext\\\\Domain\\\\PaymentUrlGenerator\\\\RequestContext constructor expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/AddDonation/AddDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationAuthorizer\\:\\:systemCanModifyDonation\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/BookDonationUseCase/BookDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Infrastructure\\\\DonationEventLogger\\:\\:log\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/BookDonationUseCase/BookDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Infrastructure\\\\DonationEventLogger\\:\\:log\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/CancelDonation/CancelDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Infrastructure\\\\DonationEventLogger\\:\\:log\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/HandlePaypalPaymentWithoutDonation/HandlePaypalPaymentWithoutDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\UseCases\\\\ModerateDonation\\\\NotificationLog\\:\\:hasSentConfirmationFor\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/ModerateDonation/ModerateDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\UseCases\\\\ModerateDonation\\\\NotificationLog\\:\\:logConfirmationSent\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/UseCases/ModerateDonation/ModerateDonationUseCase.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationAuthorizer\\:\\:canAccessDonation\\(\\) expects int, int\\|null given\\.$#"
count: 3
path: tests/Integration/DataAccess/DoctrineDonationAuthorizerTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationAuthorizer\\:\\:systemCanModifyDonation\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Integration/DataAccess/DoctrineDonationAuthorizerTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationAuthorizer\\:\\:userCanModifyDonation\\(\\) expects int, int\\|null given\\.$#"
count: 4
path: tests/Integration/DataAccess/DoctrineDonationAuthorizerTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\DataAccess\\\\DoctrineDonationEventLogger\\:\\:log\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Integration/DataAccess/DoctrineDonationEventLoggerTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Integration\\\\DataAccess\\\\DoctrineDonationEventLoggerTest\\:\\:getDonationById\\(\\) expects int, int\\|null given\\.$#"
count: 2
path: tests/Integration/DataAccess/DoctrineDonationEventLoggerTest.php

-
message: "#^Parameter \\#1 \\$donationId of static method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Data\\\\ValidDonation\\:\\:newBookedAnonymousPayPalDonationUpdate\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Integration/DataAccess/DoctrineDonationRepositoryTest.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\DataAccess\\\\DoctrineDonationRepository\\:\\:getDonationById\\(\\) expects int, int\\|null given\\.$#"
count: 4
path: tests/Integration/DataAccess/DoctrineDonationRepositoryTest.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\Donation\\:\\:assignId\\(\\) expects int, int\\|null given\\.$#"
count: 2
path: tests/Integration/DataAccess/DoctrineDonationRepositoryTest.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Integration\\\\DataAccess\\\\DoctrineDonationRepositoryTest\\:\\:getDoctrineDonationById\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Integration/DataAccess/DoctrineDonationRepositoryTest.php

-
message: "#^Call to an undefined method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationAuthorizer\\:\\:hasAuthorizedAsAdmin\\(\\)\\.$#"
count: 2
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Call to an undefined method WMDE\\\\Fundraising\\\\DonationContext\\\\Authorization\\\\DonationAuthorizer\\:\\:hasAuthorizedAsUser\\(\\)\\.$#"
count: 2
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Call to an undefined method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Repositories\\\\DonationRepository\\:\\:getStoreDonationCalls\\(\\)\\.$#"
count: 1
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Call to an undefined method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Repositories\\\\DonationRepository\\:\\:throwOnRead\\(\\)\\.$#"
count: 1
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Call to an undefined method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Repositories\\\\DonationRepository\\:\\:throwOnWrite\\(\\)\\.$#"
count: 1
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Call to an undefined method WMDE\\\\Fundraising\\\\DonationContext\\\\Infrastructure\\\\TemplateMailerInterface\\:\\:assertCalledOnceWith\\(\\)\\.$#"
count: 1
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of class WMDE\\\\Fundraising\\\\DonationContext\\\\UseCases\\\\CancelDonation\\\\CancelDonationRequest constructor expects int, int\\|null given\\.$#"
count: 11
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Repositories\\\\DonationRepository\\:\\:getDonationById\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Integration/UseCases/CancelDonation/CancelDonationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Integration\\\\UseCases\\\\UpdateDonor\\\\UpdateDonorUseCaseTest\\:\\:newUpdateDonorRequestForCompany\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Integration/UseCases/UpdateDonor/UpdateDonorUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Integration\\\\UseCases\\\\UpdateDonor\\\\UpdateDonorUseCaseTest\\:\\:newUpdateDonorRequestForPerson\\(\\) expects int, int\\|null given\\.$#"
count: 7
path: tests/Integration/UseCases/UpdateDonor/UpdateDonorUseCaseTest.php

-
message: "#^Parameter \\#1 \\$id of method WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Repositories\\\\DonationRepository\\:\\:getDonationById\\(\\) expects int, int\\|null given\\.$#"
count: 2
path: tests/Integration/UseCases/UpdateDonor/UpdateDonorUseCaseTest.php

-
message: "#^Parameter \\#2 \\$address of class WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\Donor\\\\PersonDonor constructor expects WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\Donor\\\\Address\\\\PostalAddress, WMDE\\\\Fundraising\\\\DonationContext\\\\Domain\\\\Model\\\\Address given\\.$#"
count: 1
path: tests/Unit/DataAccess/DonorFieldMapperTest.php

-
message: "#^Parameter \\#1 \\$donationId of static method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Data\\\\ValidPayPalNotificationRequest\\:\\:newDuplicatePayment\\(\\) expects int, int\\|null given\\.$#"
count: 2
path: tests/Unit/UseCases/HandlePayPalPaymentNotification/HandlePayPalPaymentCompletionNotificationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of static method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Data\\\\ValidPayPalNotificationRequest\\:\\:newInstantPayment\\(\\) expects int, int\\|null given\\.$#"
count: 4
path: tests/Unit/UseCases/HandlePayPalPaymentNotification/HandlePayPalPaymentCompletionNotificationUseCaseTest.php

-
message: "#^Parameter \\#2 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\Tests\\\\Unit\\\\UseCases\\\\HandlePayPalPaymentNotification\\\\HandlePayPalPaymentCompletionNotificationUseCaseTest\\:\\:assertEventLogContainsExpression\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: tests/Unit/UseCases/HandlePayPalPaymentNotification/HandlePayPalPaymentCompletionNotificationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\UseCases\\\\ModerateDonation\\\\ModerateDonationUseCase\\:\\:approveDonation\\(\\) expects int, int\\|null given\\.$#"
count: 7
path: tests/Unit/UseCases/ModerateDonation/ModerateDonationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\UseCases\\\\ModerateDonation\\\\ModerateDonationUseCase\\:\\:markDonationAsModerated\\(\\) expects int, int\\|null given\\.$#"
count: 5
path: tests/Unit/UseCases/ModerateDonation/ModerateDonationUseCaseTest.php

-
message: "#^Parameter \\#1 \\$donationId of method WMDE\\\\Fundraising\\\\DonationContext\\\\UseCases\\\\RestoreDonation\\\\RestoreDonationUseCase\\:\\:restoreCancelledDonation\\(\\) expects int, int\\|null given\\.$#"
count: 3
path: tests/Unit/UseCases/RestoreDonation/RestoreDonationUseCaseTest.php


2 changes: 1 addition & 1 deletion src/DataAccess/DoctrineCommentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static function ( Donation $donation ) {
->setCommentText( $donation->getComment() )
->setDonationAmount( (float)$donation->getAmount() )
->setDonationTime( $donation->getCreationTime() )
->setDonationId( $donation->getId() )
->setDonationId( $donation->getId() ?? 0 )
->freeze()
->assertNoNullFields();
},
Expand Down
25 changes: 25 additions & 0 deletions src/DataAccess/DoctrineDonationExistsChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare( strict_types = 1 );

namespace WMDE\Fundraising\DonationContext\DataAccess;

use Doctrine\ORM\EntityManager;
use WMDE\Fundraising\DonationContext\Domain\Repositories\DonationExistsChecker;

class DoctrineDonationExistsChecker implements DonationExistsChecker {

public function __construct(
private readonly EntityManager $entityManager
) {
}

public function donationExists( int $donationId ): bool {
$connection = $this->entityManager->getConnection();
$count = $connection->prepare( 'SELECT count(*) FROM spenden WHERE id=?' )
->executeQuery( [ $donationId ] )
->fetchOne();

return intval( $count ) === 1;
}
}
44 changes: 44 additions & 0 deletions src/DataAccess/DoctrineDonationIdRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare( strict_types = 1 );

namespace WMDE\Fundraising\DonationContext\DataAccess;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\ORM\EntityManager;
use WMDE\Fundraising\DonationContext\Domain\Repositories\DonationIdRepository;

class DoctrineDonationIdRepository implements DonationIdRepository {
private EntityManager $entityManager;

public function __construct( EntityManager $entityManager ) {
$this->entityManager = $entityManager;
}

public function getNewId(): int {
$connection = $this->entityManager->getConnection();

return $connection->transactional( function ( Connection $connection ): int {
$this->updateDonationId( $connection );
$result = $this->getCurrentIdResult( $connection );
$id = $result->fetchOne();

if ( $id === false ) {
throw new \RuntimeException( 'The ID generator needs a row with initial donation_id set to 0.' );
}

return intval( $id );
} );
}

private function updateDonationId( Connection $connection ): void {
$statement = $connection->prepare( 'UPDATE last_generated_donation_id SET donation_id = donation_id + 1' );
$statement->executeStatement();
}

private function getCurrentIdResult( Connection $connection ): Result {
$statement = $connection->prepare( 'SELECT donation_id FROM last_generated_donation_id LIMIT 1' );
return $statement->executeQuery();
}
}
18 changes: 11 additions & 7 deletions src/DataAccess/DoctrineDonationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WMDE\Fundraising\DonationContext\DataAccess\LegacyConverters\LegacyToDomainConverter;
use WMDE\Fundraising\DonationContext\Domain\Model\Donation;
use WMDE\Fundraising\DonationContext\Domain\Model\ModerationReason;
use WMDE\Fundraising\DonationContext\Domain\Repositories\DonationExistsChecker;
use WMDE\Fundraising\DonationContext\Domain\Repositories\DonationRepository;
use WMDE\Fundraising\DonationContext\Domain\Repositories\GetDonationException;
use WMDE\Fundraising\DonationContext\Domain\Repositories\StoreDonationException;
Expand All @@ -22,17 +23,18 @@
class DoctrineDonationRepository implements DonationRepository {

public function __construct(
private EntityManager $entityManager,
private GetPaymentUseCase $getPaymentUseCase,
private ModerationReasonRepository $moderationReasonRepository
private readonly EntityManager $entityManager,
private readonly DonationExistsChecker $donationExistsChecker,
private readonly GetPaymentUseCase $getPaymentUseCase,
private readonly ModerationReasonRepository $moderationReasonRepository
) {
}

public function storeDonation( Donation $donation ): void {
$existingModerationReasons = $this->moderationReasonRepository->getModerationReasonsThatAreAlreadyPersisted( ...$donation->getModerationReasons() );
// doctrine will persist the moderation reasons that are not yet found in the database
// and create relation entries to donation automatically
if ( $donation->getId() == null ) {
if ( !$this->donationExistsChecker->donationExists( $donation->getId() ) ) {
$this->insertDonation( $donation, $existingModerationReasons );
} else {
$this->updateDonation( $donation, $existingModerationReasons );
Expand All @@ -58,8 +60,6 @@ private function insertDonation( Donation $donation, array $existingModerationRe
} catch ( ORMException $ex ) {
throw new StoreDonationException( $ex );
}

$donation->assignId( $doctrineDonation->getId() );
}

/**
Expand Down Expand Up @@ -109,6 +109,10 @@ public function getDonationById( int $id ): ?Donation {
}

$converter = new LegacyToDomainConverter();
return $converter->createFromLegacyObject( $doctrineDonation );
try {
return $converter->createFromLegacyObject( $doctrineDonation );
} catch ( \InvalidArgumentException $ex ) {
throw new GetDonationException( $ex );
}
}
}
4 changes: 4 additions & 0 deletions src/DataAccess/LegacyConverters/LegacyToDomainConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

class LegacyToDomainConverter {
public function createFromLegacyObject( DoctrineDonation $doctrineDonation ): Donation {
if ( $doctrineDonation->getId() === null ) {
throw new \InvalidArgumentException( "Doctrine donation ID must not be null" );
}

$donor = $this->getDonor( $doctrineDonation );
$donation = new Donation(
$doctrineDonation->getId(),
Expand Down
Loading

0 comments on commit 2f50eaa

Please sign in to comment.