Created
May 29, 2024 15:51
-
-
Save dersonsena/579829e47d4c90cc23b8df1c5f636c76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class ActivateContractWithProof | |
{ | |
public function __construct( | |
private readonly ContractRepository $contractRepo, | |
private readonly CustomerRepository $customerRepo, | |
private readonly EventDispatcherInterface $eventDispatcher, | |
private readonly Validator $validator, | |
private readonly CacheSystem $cacheSystem | |
) { | |
} | |
public function execute(InputData $inputData): OutputData | |
{ | |
if (!$this->validator->validateDto($inputData)) { | |
throw new ValidationException($this->validator->getErrors()); | |
} | |
$contract = $this->contractRepo->findById($inputData->contractId); | |
if (is_null($contract)) { | |
throw new ValidationException( | |
['field' => 'contractId', 'error' => 'contractNotFound'], | |
"this contract not exists" | |
); | |
} | |
if (!$contract->isUnderAnalysis()) { | |
throw new ValidationException( | |
['field' => 'status', 'error' => 'invalidContractStatus'], | |
"This contract isn't under analysis" | |
); | |
} | |
if ($inputData->validatePaymentProof && !$contract->hasPaymentProof()) { | |
throw new ValidationException( | |
['field' => 'paymentProof', 'error' => 'withoutProof'], | |
"This contract doesn't have a payment proof" | |
); | |
} | |
if ($inputData->validatePaymentProof && !$contract->hasPaymentProofIsVerified()) { | |
throw new ValidationException( | |
['field' => 'paymentProof', 'error' => 'paymentProofNotVerified'], | |
"Payment proof has not been verified" | |
); | |
} | |
$contract->markAsActive(); | |
$contract->markCommissionPaymentAs($inputData->payCommissionToUp); | |
$customer = $contract->customer; | |
$customerAlreadyActivated = $customer->isActivatedToCommission(); | |
if (!$customerAlreadyActivated) { | |
$customer->markAsActive(); | |
} | |
$this->contractRepo->active($contract); | |
if (!$customerAlreadyActivated) { | |
$this->customerRepo->activeCustomerNetwork($customer); | |
$this->eventDispatcher->dispatch(new CustomerActivated($customer->getId())); | |
} | |
if ($customer->indicatedBy) { | |
$this->cacheSystem->destroy(CacheSystem::CUSTOMER_BALANCE . '-' . $customer->indicatedBy->getId()); | |
} | |
$this->eventDispatcher->dispatch(new ContractActivated( | |
contractId: $contract->id, | |
contractNumber: $contract->number, | |
customerId: $customer->id, | |
name: $customer->user->name, | |
email: $customer->user->email->value() | |
)); | |
return OutputData::createFromArray([ | |
'contractId' => $inputData->contractId, | |
'status' => $contract->status->value() | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment