ldc-mesbr-app/src/Domain/Models/Entities/CampaignRequest.php
2026-09-30 16:55:33 +02:00

131 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Domain\Models\Entities;
use Domain\Models\ValueObjects\Brand\BrandCode;
use Domain\Models\ValueObjects\Campaign\CampaignName;
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestStatus;
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
use Domain\Models\ValueObjects\CampaignRequest\RejectionReason;
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCoupon;
use Shared\DDD\Models\AggregateRoot;
use Shared\DDD\Models\HasTimestampIdentifier;
use Shared\Helpers\DatesHelper;
/**
* @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 $brandCode,
public CampaignName $name,
public Applicant $applicant,
public CampaignRequestType $campaignType,
public RequestedCoupon $requestedCoupon,
public CampaignRequestStatus $status,
public ?RejectionReason $rejectionReason = null,
public ?\DateTimeInterface $updatedAt = null,
) {
if ($this->status === CampaignRequestStatus::REJECTED && ! $this->rejectionReason instanceof RejectionReason) {
throw new \DomainException('A rejection reason is required when the campaign request is rejected.');
}
if ($this->status !== CampaignRequestStatus::REJECTED && $this->rejectionReason instanceof RejectionReason) {
throw new \DomainException('A rejection reason can only be set when the campaign request is rejected.');
}
}
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 create(
BrandCode $brandCode,
CampaignName $name,
Applicant $applicant,
CampaignRequestType $campaignType,
RequestedCoupon $requestedCoupon,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: CampaignRequestId::generate(),
brandCode: $brandCode,
name: $name,
applicant: $applicant,
campaignType: $campaignType,
requestedCoupon: $requestedCoupon,
status: CampaignRequestStatus::CREATED,
rejectionReason: null,
updatedAt: $updatedAt,
);
}
public static function hydrate(
CampaignRequestId $id,
BrandCode $brandCode,
CampaignName $name,
Applicant $applicant,
CampaignRequestType $campaignType,
RequestedCoupon $requestedCoupon,
CampaignRequestStatus $status,
?RejectionReason $rejectionReason = null,
?\DateTimeInterface $updatedAt = null,
): self {
return new self(
id: $id,
brandCode: $brandCode,
name: $name,
applicant: $applicant,
campaignType: $campaignType,
requestedCoupon: $requestedCoupon,
status: $status,
rejectionReason: $rejectionReason,
updatedAt: $updatedAt,
);
}
public function reject(RejectionReason $reason): self
{
if ($this->status !== CampaignRequestStatus::CREATED) {
throw new \DomainException('Only a created campaign request can be rejected.');
}
return new self(
id: $this->id,
brandCode: $this->brandCode,
name: $this->name,
applicant: $this->applicant,
campaignType: $this->campaignType,
requestedCoupon: $this->requestedCoupon,
status: CampaignRequestStatus::REJECTED,
rejectionReason: $reason,
updatedAt: DatesHelper::now(),
);
}
public function rejectionReason(): RejectionReason
{
if (! $this->rejectionReason instanceof RejectionReason) {
throw new \DomainException('Only a rejected campaign request has a rejection reason.');
}
return $this->rejectionReason;
}
}