friends-of-ddd/sequence-number-generator-symfony-bundle

Sequence Number Generator Symfony Bundle

1.0.1 2025-03-21 21:35 UTC

This package is auto-updated.

Last update: 2025-03-21 20:44:37 UTC


README

A symfony bundle implementation of friends-of-ddd/sequence-number-generator using Doctrine to generate sequence numbers.

Installation

composer require friends-of-ddd/sequence-number-generator-symfony-bundle

Enable the bundle manually in the application:

// config/bundles.php

return [
    FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Symfony\SequenceNumberGeneratorBundle::class => ['all' => true],
];

You must create a table in DB which saves sequence values. You can use either bin/console doctrine:migrations:diff (prefered) or bin/console doctrine:schema:update. Table with name sequence will be created by default. If you need another name see "Configuration" section.

Configuration

Custom table name can be configured the following way:

# config/packages/sequence_number_generator.yaml
sequence_number_generator:
  table_name: custom_sequences

For more advanced configuration use directly friends-of-ddd/sequence-number-generator-doctrine package.

Usage

Generate an incremented sequence number:


use FriendsOfDdd\SequenceNumberGenerator\Domain\SequenceNumberGeneratorInterface;

class OrderFactory
{
    public function __construct(
        private SequenceNumberGeneratorInterface $sequenceNumberGenerator // inject the service into your class 
    ) {
    }

    public function create(): void
    {
        $newOrderId = $this->sequenceNumberGenerator->generateNext('order_id');

        return new Order(
            id: $newOrderId, 
            ...
        );
    }
}

Testing

In unit tests you can use FriendsOfDdd\SequenceNumberGenerator\Infrastructure\InMemorySequenceNumberGenerator to fake real database usage.


use FriendsOfDdd\SequenceNumberGenerator\Infrastructure\InMemorySequenceNumberGenerator;

class OrderFactoryTest extends \PHPUnit\Framework\TestCase
{
    private OrderFactory $orderFactory;

    protected function setUp(): void
    {
        $this->orderFactory = new OrderFactory(
            sequenceNumberGenerator: new InMemorySequenceNumberGenerator()
        );
    }

    public function testCreateNewOrder(): void 
    {
        // arrange
        $previousOrder = $this->orderFactory->create();

        // act
        $newOrder = $this->orderFactory->create();

        // assert
        self::assertSame($previousOrder->id + 1, $newOrder->id);
    }
}


Supported PHP versions:

  • 8.1.*
  • 8.2.*
  • 8.3.*
  • 8.4.*

Supported Symfony versions

  • 5.4.48+
  • 6.4.19+
  • 7.2.4+