ldc-mesbr-app/src/Application/UseCases/Brand/Create/CreateBrand.php
2026-09-15 18:12:03 +02:00

54 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Application\UseCases\Brand\Create;
use Domain\Exceptions\BrandCodeAlreadyExistsException;
use Domain\Models\Entities\Brand;
use Domain\Ports\Repositories\BrandRepository;
use Domain\Ports\Services\PictureStorage;
use Shared\DDD\UseCases\Input as BaseInput;
use Shared\DDD\UseCases\UseCase;
use Webmozart\Assert\Assert;
/** @implements UseCase<\Application\UseCases\Brand\Create\Input, \Application\UseCases\Brand\Create\Output> */
final readonly class CreateBrand implements UseCase
{
public function __construct(
private BrandRepository $repository,
private PictureStorage $pictureStorage,
) {
}
#[\Override]
public function execute(BaseInput $input): Output
{
Assert::isInstanceOf($input, Input::class);
if ($this->repository->existsByCode($input->code)) {
throw new BrandCodeAlreadyExistsException($input->code);
}
$logoPath = $this->pictureStorage->store($input->logo);
$brand = Brand::create(
code: $input->code,
name: $input->name,
logo: $logoPath,
rcs: $input->rcs,
contact: $input->contact,
);
$this->repository->save($brand);
return new Output(
name: $brand->name,
code: $brand->code,
logo: $brand->logo,
rcs: $brand->rcs,
contact: $brand->contact,
createdAt: $brand->createdAt(),
);
}
}