Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ExpireProposalAction
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Proposals\Application\Actions;
6
7use ImovelZapAi\Proposals\Application\Services\ProposalHistoryWriter;
8use ImovelZapAi\Proposals\Domain\Enums\ProposalHistoryEvent;
9use ImovelZapAi\Proposals\Domain\Enums\ProposalStatus;
10use ImovelZapAi\Proposals\Domain\Events\ProposalExpired;
11use ImovelZapAi\Proposals\Domain\Exceptions\ProposalException;
12use ImovelZapAi\Proposals\Infrastructure\Persistence\Eloquent\ProposalModel;
13
14final class ExpireProposalAction
15{
16    public function __construct(private readonly ProposalHistoryWriter $history) {}
17
18    public function execute(ProposalModel $proposal): ProposalModel
19    {
20        if (in_array($proposal->status, [ProposalStatus::Accepted, ProposalStatus::Rejected, ProposalStatus::Expired], true)) {
21            throw ProposalException::invalidState('proposta já encerrada.');
22        }
23
24        $proposal->forceFill([
25            'status' => ProposalStatus::Expired,
26            'decided_at' => now(),
27        ])->save();
28
29        $this->history->write(
30            $proposal->tenant_id,
31            $proposal->id,
32            ProposalHistoryEvent::Expired,
33            actorType: 'system',
34            message: 'Proposta expirada.',
35        );
36
37        ProposalExpired::dispatch($proposal->tenant_id, $proposal->id);
38
39        return $proposal->fresh();
40    }
41}