73 lines
2.5 KiB
PHP
73 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Application\UseCases\Coupon\Create;
|
|
|
|
use Domain\Models\Entities\Coupon;
|
|
use Domain\Ports\Repositories\BrandRepository;
|
|
use Domain\Ports\Repositories\CouponRepository;
|
|
use Shared\DDD\UseCases\Input as BaseInput;
|
|
use Shared\DDD\UseCases\UseCase;
|
|
use Shared\DDD\UseCases\Transactional;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/** @implements UseCase<\Application\UseCases\Coupon\Create\Input, \Application\UseCases\Coupon\Create\Output> */
|
|
final readonly class CreateCoupon implements UseCase, Transactional
|
|
{
|
|
public function __construct(
|
|
private CouponRepository $couponRepository,
|
|
private BrandRepository $brandRepository,
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function execute(BaseInput $input): Output
|
|
{
|
|
Assert::isInstanceOf($input, Input::class);
|
|
|
|
if (! $this->brandRepository->existsByCode($input->brandCode)) {
|
|
throw new \DomainException(sprintf('Brand code "%s" was not found.', $input->brandCode->value));
|
|
}
|
|
|
|
if ($this->couponRepository->existsByCode($input->code)) {
|
|
throw new \DomainException(sprintf('Coupon code "%s" already exists.', $input->code->value));
|
|
}
|
|
|
|
$coupon = Coupon::create(
|
|
code: $input->code,
|
|
name: $input->name,
|
|
amount: $input->amount,
|
|
picture: $input->picture,
|
|
diffusion: $input->diffusion,
|
|
validityInformations: $input->validityInformations,
|
|
legalInformations: $input->legalInformations,
|
|
processingCode: $input->processingCode,
|
|
brandCode: $input->brandCode,
|
|
createdBy: $input->createdBy,
|
|
products: $input->products,
|
|
movements: $input->movements,
|
|
availabilityCalendar: $input->availabilityCalendar,
|
|
);
|
|
|
|
$this->couponRepository->save($coupon);
|
|
|
|
return new Output(
|
|
id: $coupon->id,
|
|
code: $coupon->code,
|
|
name: $coupon->name,
|
|
amount: $coupon->amount,
|
|
picture: $coupon->picture,
|
|
diffusion: $coupon->diffusion,
|
|
validityInformations: $coupon->validityInformations,
|
|
legalInformations: $coupon->legalInformations,
|
|
processingCode: $coupon->processingCode,
|
|
brandCode: $coupon->brandCode,
|
|
createdBy: $coupon->createdBy,
|
|
products: $coupon->products,
|
|
movements: $coupon->movements,
|
|
createdAt: $coupon->createdAt(),
|
|
availabilityCalendar: $coupon->availabilityCalendar,
|
|
);
|
|
}
|
|
}
|