56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Application\UseCases\Campaign\Update;
|
|
|
|
use Application\UseCases\Campaign\Update\Outputs\CampaignNotFound;
|
|
use Application\UseCases\Campaign\Update\Outputs\CampaignStatusUpdated;
|
|
use Application\UseCases\Campaign\Update\Outputs\CampaignUnpublicationSkipped;
|
|
use Domain\Models\Entities\Campaign;
|
|
use Domain\Ports\Repositories\CampaignRepository;
|
|
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
|
use Shared\DDD\UseCases\Input as BaseInput;
|
|
use Shared\DDD\UseCases\Output;
|
|
use Shared\DDD\UseCases\Transactional;
|
|
use Shared\DDD\UseCases\UseCase;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/** @implements UseCase<\Application\UseCases\Campaign\Update\AutoUnpublishCampaignInput, \Shared\DDD\UseCases\Output> */
|
|
final readonly class AutoUnpublishCampaign implements UseCase, Transactional
|
|
{
|
|
public function __construct(
|
|
private CampaignRepository $repository,
|
|
private CampaignAvailabilityChecker $availabilityChecker
|
|
) {
|
|
}
|
|
|
|
#[\Override]
|
|
public function execute(BaseInput $input): Output
|
|
{
|
|
Assert::isInstanceOf($input, AutoUnpublishCampaignInput::class);
|
|
|
|
$campaign = $this->repository->findByCode($input->code);
|
|
|
|
if (! $campaign instanceof Campaign) {
|
|
return new CampaignNotFound();
|
|
}
|
|
|
|
if (! $campaign->publication || $this->availabilityChecker->hasActiveCoupon($campaign)) {
|
|
return new CampaignUnpublicationSkipped();
|
|
}
|
|
|
|
$campaign = $campaign->unpublish();
|
|
$this->repository->save($campaign);
|
|
|
|
return new CampaignStatusUpdated(
|
|
name: $campaign->name,
|
|
code: $campaign->code,
|
|
publication: $campaign->publication,
|
|
printAllAtOnce: $campaign->printAllAtOnce,
|
|
coupons: $campaign->coupons,
|
|
brandCode: $campaign->brandCode,
|
|
actorId: $campaign->actorId
|
|
);
|
|
}
|
|
}
|