Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SignatureProviderRegistry
93.33% covered (success)
93.33%
14 / 15
50.00% covered (danger)
50.00%
1 / 2
8.02
0.00% covered (danger)
0.00%
0 / 1
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
7.02
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Signatures\Application\Services;
6
7use ImovelZapAi\Signatures\Domain\Enums\SignatureProviderKey;
8use ImovelZapAi\Signatures\Domain\Exceptions\SignatureException;
9use ImovelZapAi\Signatures\Domain\Ports\SignatureProviderInterface;
10
11final class SignatureProviderRegistry
12{
13    /** @var array<string, SignatureProviderInterface> */
14    private array $providers = [];
15
16    public function register(SignatureProviderInterface $provider): void
17    {
18        $this->providers[$provider->key()->value] = $provider;
19    }
20
21    public function get(SignatureProviderKey|string $key): SignatureProviderInterface
22    {
23        if ($key instanceof SignatureProviderKey) {
24            $providerKey = $key;
25        } else {
26            $providerKey = SignatureProviderKey::tryFrom($key);
27            if ($providerKey === null) {
28                throw SignatureException::providerUnavailable("Provedor de assinatura desconhecido: {$key}.");
29            }
30        }
31
32        if ($providerKey !== SignatureProviderKey::Native) {
33            $config = config('signatures.providers.'.$providerKey->value, []);
34            if (empty($config['enabled']) || empty($config['api_key'])) {
35                throw SignatureException::providerUnavailable(
36                    "Provedor {$providerKey->label()} não configurado neste ambiente.",
37                );
38            }
39        }
40
41        if (! isset($this->providers[$providerKey->value])) {
42            throw SignatureException::providerUnavailable();
43        }
44
45        return $this->providers[$providerKey->value];
46    }
47}