ldc-mesbr-app/src/Application/UseCases/Coupon/Find/FindCoupon.php
2026-10-14 11:53:24 +02:00

64 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Application\UseCases\Coupon\Find;
use Application\UseCases\Coupon\Find\Output\Output;
use Application\UseCases\Coupon\Find\Output\CouponFound;
use Application\UseCases\Coupon\Find\Output\CouponNotFound;
use Domain\Models\Entities\Coupon;
use Domain\Ports\Repositories\BrandRepository;
use Domain\Ports\Repositories\CampaignRepository;
use Domain\Ports\Repositories\CouponRepository;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\DDD\UseCases\UseCase;
use Webmozart\Assert\Assert;
/** @implements UseCase<\Application\UseCases\Coupon\Find\Input, \Application\UseCases\Coupon\Find\Output\Output> */
final readonly class FindCoupon implements UseCase
{
public function __construct(
private CouponRepository $couponRepository,
private BrandRepository $brandRepository,
private CampaignRepository $campaignRepository,
) {
}
#[\Override]
public function execute(BaseInput $input): Output
{
Assert::isInstanceOf($input, Input::class);
$coupon = $this->couponRepository->findByCode($input->code);
if (! $coupon instanceof Coupon) {
return new CouponNotFound();
}
if (! $this->brandRepository->existsByCode($coupon->brandCode)) {
throw new \DomainException(sprintf('Brand code "%s" was not found.', $coupon->brandCode->value));
}
$campaigns = $this->campaignRepository->findByCouponCode($coupon->code);
return new CouponFound(
code: $coupon->code,
name: $coupon->name,
amount: $coupon->amount,
picture: $coupon->picture,
diffusion: $coupon->diffusion,
availabilityCalendar: $coupon->availabilityCalendar,
validityInformations: $coupon->validityInformations,
legalInformations: $coupon->legalInformations,
processingCode: $coupon->processingCode,
brandCode: $coupon->brandCode,
createdBy: $coupon->createdBy,
products: $coupon->products,
movements: $coupon->movements,
quantity: $coupon->quantity(),
campaigns: $campaigns,
createdAt: $coupon->createdAt(),
updatedAt: $coupon->updatedAt(),
);
}
}