42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Sales\Application\UseCases;
|
|
|
|
use App\Modules\Sales\Application\Contracts\ReceiptMailer;
|
|
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
|
|
use App\Modules\Sales\Domain\Exceptions\QuickSaleNotFound;
|
|
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
|
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
|
|
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
|
|
|
/**
|
|
* Emails a sale's receipt again, to the stored address or a corrected one,
|
|
* which replaces it on the sale. Who may do this is checked by the caller.
|
|
*/
|
|
final class ResendQuickSaleReceipt
|
|
{
|
|
public function __construct(
|
|
private QuickSaleRepository $sales,
|
|
private ReceiptMailer $mailer,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws QuickSaleNotFound
|
|
* @throws InvalidEmailAddress
|
|
*/
|
|
public function execute(int $saleId, string $email): void
|
|
{
|
|
$address = new EmailAddress($email);
|
|
|
|
$this->transactions->run(function () use ($saleId, $address): void {
|
|
$this->sales->findById($saleId) ?? throw new QuickSaleNotFound($saleId);
|
|
$this->sales->changeReceiptEmail($saleId, $address);
|
|
});
|
|
|
|
$this->mailer->send($saleId);
|
|
}
|
|
}
|