-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from wmde/int-donation-id
Remove nullable ID from domain Donation entity
- Loading branch information
Showing
41 changed files
with
695 additions
and
650 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
config/DoctrineClassMapping/WMDE.Fundraising.DonationContext.Domain.Model.DonationId.dcm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.