Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.00% covered (warning)
88.00%
22 / 25
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
StubIntegratedSignatureProvider
88.00% covered (warning)
88.00%
22 / 25
85.71% covered (warning)
85.71%
6 / 7
13.29
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 key
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 send
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
 completeSigner
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 cancel
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 outboundMessenger
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isEnabled
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Signatures\Infrastructure\Providers\Drivers;
6
7use Illuminate\Support\Str;
8use ImovelZapAi\Conversations\Application\Actions\SendOutboundMessageAction;
9use ImovelZapAi\Signatures\Application\Services\NativeSignerCompletionService;
10use ImovelZapAi\Signatures\Domain\Enums\SignatureProviderKey;
11use ImovelZapAi\Signatures\Domain\Exceptions\SignatureException;
12use ImovelZapAi\Signatures\Domain\Ports\SignatureProviderInterface;
13use ImovelZapAi\Signatures\Infrastructure\Persistence\Eloquent\SignatureRequestModel;
14use ImovelZapAi\Signatures\Infrastructure\Persistence\Eloquent\SignerModel;
15use ImovelZapAi\Signatures\Infrastructure\Providers\Drivers\Concerns\NotifiesSignersViaWhatsapp;
16
17final class StubIntegratedSignatureProvider implements SignatureProviderInterface
18{
19    use NotifiesSignersViaWhatsapp;
20
21    public function __construct(
22        private readonly SignatureProviderKey $key,
23        private readonly SendOutboundMessageAction $sendText,
24        private readonly NativeSignerCompletionService $completion,
25    ) {
26        if ($key === SignatureProviderKey::Native) {
27            throw new \InvalidArgumentException('StubIntegratedSignatureProvider não suporta provider native.');
28        }
29    }
30
31    public function key(): SignatureProviderKey
32    {
33        return $this->key;
34    }
35
36    public function send(SignatureRequestModel $request): void
37    {
38        if (! $this->isEnabled()) {
39            throw SignatureException::providerUnavailable(
40                "Provedor {$this->key->label()} não configurado neste ambiente.",
41            );
42        }
43
44        $request->forceFill([
45            'provider_external_id' => 'stub-'.(string) Str::uuid(),
46        ])->save();
47
48        $this->notifySigners($request);
49    }
50
51    public function completeSigner(SignatureRequestModel $request, SignerModel $signer, array $payload): void
52    {
53        if (! $this->isEnabled()) {
54            throw SignatureException::providerUnavailable(
55                "Provedor {$this->key->label()} não configurado neste ambiente.",
56            );
57        }
58
59        $this->completion->complete($request, $signer, $payload);
60    }
61
62    public function cancel(SignatureRequestModel $request): void
63    {
64        if (! $this->isEnabled()) {
65            throw SignatureException::providerUnavailable(
66                "Provedor {$this->key->label()} não configurado neste ambiente.",
67            );
68        }
69
70        if ($request->provider_external_id === null) {
71            $request->forceFill(['provider_external_id' => 'stub-cancelled-'.(string) Str::uuid()])->save();
72        }
73    }
74
75    protected function outboundMessenger(): SendOutboundMessageAction
76    {
77        return $this->sendText;
78    }
79
80    private function isEnabled(): bool
81    {
82        $config = config('signatures.providers.'.$this->key->value, []);
83
84        return ! empty($config['enabled']) && ! empty($config['api_key']);
85    }
86}