feat: 🔨 on the campaign entity/type

This commit is contained in:
K Muller 2026-09-22 13:01:30 +02:00 • committed by Corentin Huet
parent dbcaa60dce
commit b4d45ec716
14 changed files with 635 additions and 0 deletions

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Domain\Enums\Campaign;
use Shared\Enums\BaseEnum;
enum CampaignStatus: int
{
/** @use \Shared\Enums\BaseEnum<int> */
use BaseEnum;
case CREATED = 1;
case TRANSFORMED = 2;
case REJECTED = 3;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::CREATED->name => 'Crée',
self::TRANSFORMED->name => 'Transformé',
self::REJECTED->name => 'Rejeté'
];
}

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Domain\Enums\Campaign;
use Shared\Enums\BaseEnum;
enum CampaignType: int
{
/** @use \Shared\Enums\BaseEnum<int> */
use BaseEnum;
case CLUB = 1;
case GAME = 2;
/**
* Optional labels definition
*
* @phpstan-ignore-next-line
*/
private const LABELS = [
self::CLUB->name => 'CLUB',
self::GAME->name => 'GAME',
];
}

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Enums\Campaign\CampaignStatus;
use Domain\Enums\Campaign\CampaignType;
use Domain\Enums\Country;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
use Domain\Models\ValueObjects\PrintRules;
use Shared\DDD\Models\AggregateRoot;
use Shared\DDD\Models\HasTimestampIdentifier;
/**
* @implements \Shared\DDD\Models\AggregateRoot<\Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId>
*/
final readonly class CampaignRequest implements AggregateRoot, HasTimestampIdentifier
{
private function __construct(
public CampaignRequestId $id,
public BrandCode $code,
public Applicant $applicant,
public Country $country,
public CampaignType $campaignType,
public ?PrintRules $printRules,
public RequestedCoupon $requestedCoupon,
public CampaignStatus $status,
public ?\DateTimeInterface $updatedAt = null,
) {}
public function id(): CampaignRequestId
{
return $this->id;
}
public function createdAt(): \DateTimeInterface
{
return $this->id->toDateTime();
}
public function updatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public static function hydrate(
CampaignRequestId $id,
BrandCode $code,
Applicant $applicant,
Country $country,
CampaignType $campaignType,
?PrintRules $printRules,
RequestedCoupon $requestedCoupon,
CampaignStatus $status,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: $id,
code: $code,
applicant: $applicant,
country: $country,
campaignType: $campaignType,
printRules: $printRules,
requestedCoupon: $requestedCoupon,
status: $status,
updatedAt: $updatedAt,
);
}
}

View file

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Models\ValueObjects\MoneyAmount;
use Domain\Models\ValueObjects\Period;
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponId;
use Shared\DDD\Models\AggregateRoot;
use Shared\DDD\Models\HasTimestampIdentifier;
/**
* @implements \Shared\DDD\Models\AggregateRoot<\Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponId>
*/
final readonly class RequestedCoupon implements AggregateRoot, HasTimestampIdentifier
{
private function __construct(
public RequestedCouponId $id,
public string $name,
public string $productName,
public int $quantity,
public int $warningThresholdQuantity,
public Period $period,
public MoneyAmount $moneyAmount,
// FROM COUPONS
public $processingCode,
public $couponPicture,
public $gs1,
public $validity,
public ?\DateTimeInterface $updatedAt = null,
) {
}
public function id(): RequestedCouponId
{
return $this->id;
}
public function createdAt(): \DateTimeInterface
{
return $this->id->toDateTime();
}
public function updatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public static function hydrate(
RequestedCouponId $id,
string $name,
string $productName,
int $quantity,
int $warningThresholdQuantity,
Period $period,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: $id,
name: $name,
productName: $productName,
quantity: $quantity,
warningThresholdQuantity: $warningThresholdQuantity,
period: $period,
updatedAt: $updatedAt,
);
}
}

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\CampaignRequest;
use Shared\ValueObjects\Email;
final readonly class Applicant
{
public function __construct(
public string $firstname,
public string $lastname,
public Email $email,
) {
$trimmedFirstname = trim($firstname);
if ($trimmedFirstname === '') {
throw new \DomainException('Applicant firstname cannot be empty.');
}
if (mb_strlen($trimmedFirstname) > 200) {
throw new \DomainException('Applicant firstname exceeds the maximum allowed length.');
}
$trimmedLastname = trim($lastname);
if ($trimmedLastname === '') {
throw new \DomainException('Applicant lastname cannot be empty.');
}
if (mb_strlen($trimmedLastname) > 200) {
throw new \DomainException('Applicant lastname exceeds the maximum allowed length.');
}
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\CampaignRequest;
use Ramsey\Uuid\Uuid;
use Shared\DDD\Models\Uuidv7EntityId;
final readonly class CampaignRequestId extends Uuidv7EntityId
{
public static function generate(): static
{
return new self(Uuid::uuid7());
}
}

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects;
final readonly class PrintRules
{
public function __construct(
public ?int $maxPerCustomer = null,
public ?int $cooldownDays = null,
) {
}
}

View file

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Domain\Models\ValueObjects\RequestedCoupon;
use Ramsey\Uuid\Uuid;
use Shared\DDD\Models\Uuidv7EntityId;
final readonly class RequestedCouponId extends Uuidv7EntityId
{
public static function generate(): static
{
return new self(Uuid::uuid7());
}
}

View file

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Enums\Campaign;
use Domain\Enums\Campaign\CampaignStatus;
covers(CampaignStatus::class);
describe('campaign status', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(CampaignStatus::CREATED->value())->toBe(1)
->and(CampaignStatus::TRANSFORMED->value())->toBe(2)
->and(CampaignStatus::REJECTED->value())->toBe(3);
});
it('exposes its case name', function (): void {
// THEN
expect(CampaignStatus::CREATED->name())->toBe('CREATED');
});
it('returns the defined label', function (): void {
// THEN
expect(CampaignStatus::CREATED->label())->toBe('Crée')
->and(CampaignStatus::TRANSFORMED->label())->toBe('Transformé')
->and(CampaignStatus::REJECTED->label())->toBe('Rejeté');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(CampaignStatus::toLabels())->toBe([
'CREATED' => 'Crée',
'TRANSFORMED' => 'Transformé',
'REJECTED' => 'Rejeté',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(CampaignStatus::toValues())->toBe([
'CREATED' => 1,
'TRANSFORMED' => 2,
'REJECTED' => 3,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(CampaignStatus::make(1))->toBe(CampaignStatus::CREATED);
});
it('makes an instance from a label', function (): void {
// THEN
expect(CampaignStatus::make('Transformé', 'label'))->toBe(CampaignStatus::TRANSFORMED);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(CampaignStatus::make(CampaignStatus::REJECTED))->toBe(CampaignStatus::REJECTED);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(CampaignStatus::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(CampaignStatus::CREATED->equals(CampaignStatus::CREATED))->toBeTrue()
->and(CampaignStatus::CREATED->equals(CampaignStatus::REJECTED))->toBeFalse()
->and(CampaignStatus::CREATED->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(CampaignStatus::CREATED->jsonSerialize())->toBe(1)
->and(json_encode(CampaignStatus::CREATED))->toBe('1');
});
});

View file

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Enums\Campaign;
use Domain\Enums\Campaign\CampaignType;
covers(CampaignType::class);
describe('campaign type', function (): void {
it('has the expected cases with their backing values', function (): void {
// THEN
expect(CampaignType::CLUB->value())->toBe(1)
->and(CampaignType::GAME->value())->toBe(2);
});
it('exposes its case name', function (): void {
// THEN
expect(CampaignType::CLUB->name())->toBe('CLUB');
});
it('returns the defined label', function (): void {
// THEN
expect(CampaignType::CLUB->label())->toBe('CLUB')
->and(CampaignType::GAME->label())->toBe('GAME');
});
it('lists all labels keyed by case name', function (): void {
// THEN
expect(CampaignType::toLabels())->toBe([
'CLUB' => 'CLUB',
'GAME' => 'GAME',
]);
});
it('lists all values keyed by case name', function (): void {
// THEN
expect(CampaignType::toValues())->toBe([
'CLUB' => 1,
'GAME' => 2,
]);
});
it('makes an instance from its backing value', function (): void {
// THEN
expect(CampaignType::make(1))->toBe(CampaignType::CLUB);
});
it('makes an instance from a label', function (): void {
// THEN
expect(CampaignType::make('GAME', 'label'))->toBe(CampaignType::GAME);
});
it('returns the same instance when already an enum case', function (): void {
// THEN
expect(CampaignType::make(CampaignType::CLUB))->toBe(CampaignType::CLUB);
});
it('returns null for an unknown value', function (): void {
// THEN
expect(CampaignType::make(99))->toBeNull();
});
it('implements equals correctly', function (): void {
// THEN
expect(CampaignType::CLUB->equals(CampaignType::CLUB))->toBeTrue()
->and(CampaignType::CLUB->equals(CampaignType::GAME))->toBeFalse()
->and(CampaignType::CLUB->equals(null))->toBeFalse();
});
it('serializes to json as its backing value', function (): void {
// THEN
expect(CampaignType::CLUB->jsonSerialize())->toBe(1)
->and(json_encode(CampaignType::CLUB))->toBe('1');
});
});

View file

@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\CampaignRequest;
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
use Shared\ValueObjects\Email;
covers(Applicant::class);
describe('applicant', function (): void {
it('creates a valid applicant', function (): void {
// GIVEN
$firstname = 'John';
$lastname = 'Doe';
$email = new Email('john.doe@example.com');
// WHEN
$applicant = new Applicant($firstname, $lastname, $email);
// THEN
expect($applicant)->toBeInstanceOf(Applicant::class)
->and($applicant->firstname)->toBe($firstname)
->and($applicant->lastname)->toBe($lastname)
->and($applicant->email)->toBe($email);
});
it('rejects an empty or whitespace-only firstname', function (string $firstname): void {
// WHEN
$action = fn (): Applicant => new Applicant($firstname, 'Doe', new Email('john.doe@example.com'));
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant firstname cannot be empty.');
})->with([
'empty' => '',
'spaces' => ' ',
]);
it('rejects a firstname that exceeds the maximum allowed length', function (): void {
// GIVEN
$firstname = str_repeat('a', 201);
// WHEN
$action = fn (): Applicant => new Applicant($firstname, 'Doe', new Email('john.doe@example.com'));
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant firstname exceeds the maximum allowed length.');
});
it('accepts a firstname of exactly the maximum length', function (): void {
// GIVEN
$firstname = str_repeat('a', 200);
// WHEN
$applicant = new Applicant($firstname, 'Doe', new Email('john.doe@example.com'));
// THEN
expect(mb_strlen($applicant->firstname))->toBe(200);
});
it('rejects an empty or whitespace-only lastname', function (string $lastname): void {
// WHEN
$action = fn (): Applicant => new Applicant('John', $lastname, new Email('john.doe@example.com'));
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant lastname cannot be empty.');
})->with([
'empty' => '',
'spaces' => ' ',
]);
it('rejects a lastname that exceeds the maximum allowed length', function (): void {
// GIVEN
$lastname = str_repeat('a', 201);
// WHEN
$action = fn (): Applicant => new Applicant('John', $lastname, new Email('john.doe@example.com'));
// THEN
expect($action)->toThrow(\DomainException::class, 'Applicant lastname exceeds the maximum allowed length.');
});
it('accepts a lastname of exactly the maximum length', function (): void {
// GIVEN
$lastname = str_repeat('a', 200);
// WHEN
$applicant = new Applicant('John', $lastname, new Email('john.doe@example.com'));
// THEN
expect(mb_strlen($applicant->lastname))->toBe(200);
});
});

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\CampaignRequest;
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
covers(CampaignRequestId::class);
describe('campaign request id', function (): void {
it('generates a unique campaign request id', function (): void {
// WHEN
$id = CampaignRequestId::generate();
// THEN
expect($id)->toBeInstanceOf(CampaignRequestId::class);
});
it('generates different ids on each call', function (): void {
// WHEN
$first = CampaignRequestId::generate();
$second = CampaignRequestId::generate();
// THEN
expect((string) $first)->not->toBe((string) $second);
});
});

View file

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects;
use Domain\Models\ValueObjects\PrintRules;
covers(PrintRules::class);
describe('print rules', function (): void {
it('creates print rules with both fields set', function (): void {
// WHEN
$printRules = new PrintRules(maxPerCustomer: 3, cooldownDays: 30);
// THEN
expect($printRules)->toBeInstanceOf(PrintRules::class)
->and($printRules->maxPerCustomer)->toBe(3)
->and($printRules->cooldownDays)->toBe(30);
});
it('creates print rules with only maxPerCustomer set', function (): void {
// WHEN
$printRules = new PrintRules(maxPerCustomer: 5);
// THEN
expect($printRules->maxPerCustomer)->toBe(5)
->and($printRules->cooldownDays)->toBeNull();
});
it('creates print rules with only cooldownDays set', function (): void {
// WHEN
$printRules = new PrintRules(cooldownDays: 45);
// THEN
expect($printRules->maxPerCustomer)->toBeNull()
->and($printRules->cooldownDays)->toBe(45);
});
it('creates print rules with both fields null by default', function (): void {
// WHEN
$printRules = new PrintRules();
// THEN
expect($printRules->maxPerCustomer)->toBeNull()
->and($printRules->cooldownDays)->toBeNull();
});
});

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\RequestedCoupon;
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponId;
covers(RequestedCouponId::class);
describe('requested coupon id', function (): void {
it('generates a unique requested coupon id', function (): void {
// WHEN
$id = RequestedCouponId::generate();
// THEN
expect($id)->toBeInstanceOf(RequestedCouponId::class);
});
it('generates different ids on each call', function (): void {
// WHEN
$first = RequestedCouponId::generate();
$second = RequestedCouponId::generate();
// THEN
expect((string) $first)->not->toBe((string) $second);
});
});