feat: ✨ create Coupon entity and associated VOs
This commit is contained in:
parent
f8c2dd2a3f
commit
6c8301f158
15 changed files with 501 additions and 0 deletions
28
src/Domain/Enums/BarcodeFormat.php
Normal file
28
src/Domain/Enums/BarcodeFormat.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Enums;
|
||||
|
||||
use Shared\Enums\BaseEnum;
|
||||
|
||||
enum BarcodeFormat: int
|
||||
{
|
||||
/** @use BaseEnum<int> */
|
||||
use BaseEnum;
|
||||
|
||||
case EAN13 = 1;
|
||||
case CODE128 = 2;
|
||||
case GS1_128 = 3;
|
||||
|
||||
/**
|
||||
* Optional labels definition
|
||||
*
|
||||
* @phpstan-ignore-next-line
|
||||
*/
|
||||
private const LABELS = [
|
||||
self::EAN13->name => 'EAN',
|
||||
self::CODE128->name => 'CODE 128',
|
||||
self::GS1_128->name => 'GS1_128',
|
||||
];
|
||||
}
|
||||
132
src/Domain/Models/Entities/Coupon.php
Normal file
132
src/Domain/Models/Entities/Coupon.php
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\Entities;
|
||||
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponAvailabilityCalendar;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponDiffusion;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponId;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponLegalInformation;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponProduct;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponValidity;
|
||||
use Domain\Models\ValueObjects\CouponName;
|
||||
use Domain\Models\ValueObjects\CouponPicture;
|
||||
use Domain\Models\ValueObjects\CouponProductName;
|
||||
use Domain\Models\ValueObjects\GS1;
|
||||
use Domain\Models\ValueObjects\MoneyAmount;
|
||||
use Domain\Models\ValueObjects\ProcessingCode;
|
||||
use Shared\DDD\Models\AggregateRoot;
|
||||
use Shared\DDD\Models\HasTimestampIdentifier;
|
||||
|
||||
/**
|
||||
* @implements \Shared\DDD\Models\AggregateRoot<\Domain\Models\ValueObjects\Coupon\CouponId>
|
||||
*/
|
||||
final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
||||
{
|
||||
private function __construct(
|
||||
public CouponId $id,
|
||||
public CouponName $name,
|
||||
public bool $status,
|
||||
public CouponProductName $productName,
|
||||
public MoneyAmount $amount,
|
||||
public CouponPicture $picture,
|
||||
public CouponProduct $product,
|
||||
public CouponDiffusion $diffusion,
|
||||
public CouponAvailabilityCalendar $availability,
|
||||
public CouponValidity $validity,
|
||||
public CouponLegalInformation $legal,
|
||||
public GS1 $gtin,
|
||||
public ProcessingCode $code,
|
||||
public BrandCode $brandCode,
|
||||
public ?\DateTimeInterface $updatedAt = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public function id(): CouponId
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function createdAt(): \DateTimeInterface
|
||||
{
|
||||
return $this->id->toDateTime();
|
||||
}
|
||||
|
||||
public function updatedAt(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->updatedAt;
|
||||
}
|
||||
|
||||
public static function create(
|
||||
CouponName $name,
|
||||
bool $status,
|
||||
CouponProductName $productName,
|
||||
MoneyAmount $amount,
|
||||
CouponPicture $picture,
|
||||
CouponProduct $product,
|
||||
CouponDiffusion $diffusion,
|
||||
CouponAvailabilityCalendar $availability,
|
||||
CouponValidity $validity,
|
||||
CouponLegalInformation $legal,
|
||||
GS1 $gtin,
|
||||
ProcessingCode $code,
|
||||
BrandCode $brandCode,
|
||||
?\DateTimeInterface $updatedAt = null,
|
||||
): Coupon {
|
||||
return new self(
|
||||
id: CouponId::generate(),
|
||||
name: $name,
|
||||
status: $status,
|
||||
productName: $productName,
|
||||
amount: $amount,
|
||||
picture: $picture,
|
||||
product: $product,
|
||||
diffusion: $diffusion,
|
||||
availability: $availability,
|
||||
validity: $validity,
|
||||
legal: $legal,
|
||||
gtin: $gtin,
|
||||
code: $code,
|
||||
brandCode: $brandCode,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
public static function hydrate(
|
||||
CouponId $id,
|
||||
CouponName $name,
|
||||
bool $status,
|
||||
CouponProductName $productName,
|
||||
MoneyAmount $amount,
|
||||
CouponPicture $picture,
|
||||
CouponProduct $product,
|
||||
CouponDiffusion $diffusion,
|
||||
CouponAvailabilityCalendar $availability,
|
||||
CouponValidity $validity,
|
||||
CouponLegalInformation $legal,
|
||||
GS1 $gtin,
|
||||
ProcessingCode $code,
|
||||
BrandCode $brandCode,
|
||||
?\DateTimeInterface $updatedAt = null,
|
||||
): self {
|
||||
return new self(
|
||||
id: $id,
|
||||
name: $name,
|
||||
status: $status,
|
||||
productName: $productName,
|
||||
amount: $amount,
|
||||
picture: $picture,
|
||||
product: $product,
|
||||
diffusion: $diffusion,
|
||||
availability: $availability,
|
||||
validity: $validity,
|
||||
legal: $legal,
|
||||
gtin: $gtin,
|
||||
code: $code,
|
||||
brandCode: $brandCode,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
use Shared\DDD\Models\Equatable;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
|
||||
final readonly class CouponAvailabilityCalendar implements Equatable
|
||||
{
|
||||
public function __construct(
|
||||
/** @var Period[] */
|
||||
public array $periods,
|
||||
) {
|
||||
if ($this->periods === []) {
|
||||
throw new \DomainException('Diffusion requires at least one period.');
|
||||
}
|
||||
}
|
||||
|
||||
public function isActiveAt(DateTime $in): bool
|
||||
{
|
||||
foreach ($this->periods as $period) {
|
||||
if ($in >= $period->start && $in <= $period->end) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function beginsAt(): DateTime
|
||||
{
|
||||
return min(array_map(fn (Period $period) => $period->start, $this->periods));
|
||||
}
|
||||
|
||||
public function endsAt(): DateTime
|
||||
{
|
||||
return max(array_map(fn (Period $period) => $period->end, $this->periods));
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->periods == $other->periods;
|
||||
}
|
||||
}
|
||||
19
src/Domain/Models/ValueObjects/Coupon/CouponDiffusion.php
Normal file
19
src/Domain/Models/ValueObjects/Coupon/CouponDiffusion.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
|
||||
final readonly class CouponDiffusion
|
||||
{
|
||||
public function __construct(
|
||||
/** @var Period[] */
|
||||
public array $periods,
|
||||
) {
|
||||
if ($this->periods === []) {
|
||||
throw new \DomainException('Diffusion requires at least one period.');
|
||||
}
|
||||
}
|
||||
}
|
||||
16
src/Domain/Models/ValueObjects/Coupon/CouponId.php
Normal file
16
src/Domain/Models/ValueObjects/Coupon/CouponId.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\DDD\Models\Uuidv7EntityId;
|
||||
|
||||
final readonly class CouponId extends Uuidv7EntityId
|
||||
{
|
||||
public static function generate(): self
|
||||
{
|
||||
return new self(Uuid::uuid7());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Domain\Enums\Country;
|
||||
use Shared\DDD\Models\Equatable;
|
||||
|
||||
final readonly class CouponLegalInformation implements Equatable
|
||||
{
|
||||
public function __construct(
|
||||
public string $legalText,
|
||||
public Country $forLocale
|
||||
) {
|
||||
$trimmed = trim($this->legalText);
|
||||
|
||||
if ($trimmed === '') {
|
||||
throw new \DomainException('Legal text cannot be empty.');
|
||||
}
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->legalText === $other->legalText && $this->forLocale->equals($other->forLocale);
|
||||
}
|
||||
}
|
||||
20
src/Domain/Models/ValueObjects/Coupon/CouponProduct.php
Normal file
20
src/Domain/Models/ValueObjects/Coupon/CouponProduct.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Domain\Models\ValueObjects\Coupon\CouponProductInfo;
|
||||
use Domain\Models\ValueObjects\IncoCode;
|
||||
|
||||
final readonly class CouponProduct
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
/** @var IncoCode[] */
|
||||
public array $incoCodes,
|
||||
/** @var CouponProductInfo[] */
|
||||
public array $labels,
|
||||
) {
|
||||
}
|
||||
}
|
||||
34
src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php
Normal file
34
src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Domain\Enums\Country;
|
||||
use Shared\DDD\Models\Equatable;
|
||||
|
||||
final readonly class CouponProductInfo implements Equatable
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $range,
|
||||
public string $validityText,
|
||||
public Country $forLocale
|
||||
) {
|
||||
$trimmed = trim($this->name);
|
||||
|
||||
if ($trimmed === '') {
|
||||
throw new \DomainException('Product Info name cannot be empty.');
|
||||
}
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self
|
||||
&& $this->name === $other->name
|
||||
&& $this->range === $other->range
|
||||
&& $this->validityText === $other->validityText
|
||||
&& $this->forLocale->equals($other->forLocale);
|
||||
}
|
||||
}
|
||||
26
src/Domain/Models/ValueObjects/Coupon/CouponValidity.php
Normal file
26
src/Domain/Models/ValueObjects/Coupon/CouponValidity.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects\Coupon;
|
||||
|
||||
use Shared\DDD\Models\Equatable;
|
||||
use Shared\ValueObjects\DateTime;
|
||||
|
||||
final readonly class CouponValidity implements Equatable
|
||||
{
|
||||
public function __construct(
|
||||
public int $days,
|
||||
public DateTime $until
|
||||
) {
|
||||
if ($this->days <= 0) {
|
||||
throw new \DomainException('Coupon Validity days must be positive.');
|
||||
}
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->days === $other->days && $this->until == $other->until;
|
||||
}
|
||||
}
|
||||
18
src/Domain/Models/ValueObjects/CouponName.php
Normal file
18
src/Domain/Models/ValueObjects/CouponName.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
final readonly class CouponName
|
||||
{
|
||||
public function __construct(
|
||||
public string $value,
|
||||
) {
|
||||
$trimmed = trim($value);
|
||||
|
||||
if ($trimmed === '') {
|
||||
throw new \DomainException('Coupon name cannot be empty.');
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/Domain/Models/ValueObjects/CouponPicture.php
Normal file
23
src/Domain/Models/ValueObjects/CouponPicture.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
use Domain\Models\ValueObjects\PicturePath;
|
||||
use Shared\DDD\Models\Equatable;
|
||||
|
||||
final readonly class CouponPicture implements Equatable
|
||||
{
|
||||
public function __construct(
|
||||
public PicturePath $product,
|
||||
public PicturePath $thumbnail
|
||||
) {
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->product->equals($other->product) && $this->thumbnail->equals($other->thumbnail);
|
||||
}
|
||||
}
|
||||
18
src/Domain/Models/ValueObjects/CouponProductName.php
Normal file
18
src/Domain/Models/ValueObjects/CouponProductName.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
final readonly class CouponProductName
|
||||
{
|
||||
public function __construct(
|
||||
public string $value,
|
||||
) {
|
||||
$trimmed = trim($value);
|
||||
|
||||
if ($trimmed === '') {
|
||||
throw new \DomainException('Coupon product name cannot be empty.');
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/Domain/Models/ValueObjects/GS1.php
Normal file
33
src/Domain/Models/ValueObjects/GS1.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
use Domain\Enums\BarcodeFormat;
|
||||
use Shared\DDD\Models\Equatable;
|
||||
|
||||
final readonly class GS1 implements \Stringable, Equatable
|
||||
{
|
||||
public function __construct(
|
||||
public string $value,
|
||||
public BarcodeFormat $format
|
||||
) {
|
||||
$trimmed = trim($this->value);
|
||||
|
||||
if ($trimmed === '') {
|
||||
throw new \DomainException('GS1 value cannot be empty.');
|
||||
}
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->value === $other->value && $this->format === $other->format;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
27
src/Domain/Models/ValueObjects/IncoCode.php
Normal file
27
src/Domain/Models/ValueObjects/IncoCode.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
use Shared\DDD\Models\Equatable;
|
||||
|
||||
final readonly class IncoCode implements Equatable
|
||||
{
|
||||
/**
|
||||
* @throws \DomainException
|
||||
*/
|
||||
public function __construct(
|
||||
public int $value,
|
||||
) {
|
||||
if ($this->value <= 0) {
|
||||
throw new \DomainException('Inco Code must be a positive integer.');
|
||||
}
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->value === $other->value;
|
||||
}
|
||||
}
|
||||
31
src/Domain/Models/ValueObjects/ProcessingCode.php
Normal file
31
src/Domain/Models/ValueObjects/ProcessingCode.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Models\ValueObjects;
|
||||
|
||||
use Shared\DDD\Models\Equatable;
|
||||
|
||||
final readonly class ProcessingCode implements \Stringable, Equatable
|
||||
{
|
||||
public function __construct(
|
||||
public string $value,
|
||||
) {
|
||||
$trimmed = trim($this->value);
|
||||
|
||||
if ($trimmed === '') {
|
||||
throw new \DomainException('GS1 value cannot be empty.');
|
||||
}
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function equals(mixed $other): bool
|
||||
{
|
||||
return $other instanceof self && $this->value === $other->value;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue