Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlivoSmsProvider
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 send
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2// App/Services/Communication/Providers/Sms/Global/PlivoSmsProvider.php
3
4namespace App\Services\Communication\Providers\Sms\Global;
5
6use App\Services\Communication\Contracts\SmsProviderInterface;
7
8class PlivoSmsProvider implements SmsProviderInterface
9{
10    private string $apiUrl;
11    private string $authId;
12    private string $authToken;
13    private string $senderId;
14
15    public function __construct()
16    {
17        $config = config('CommunicationProviders');
18
19        $this->authId    = $config->plivo['auth_id']    ?? '';
20        $this->authToken = $config->plivo['auth_token'] ?? '';
21        $this->senderId  = $config->plivo['sender_id']  ?? '';
22        $this->apiUrl    = $config->plivo['api_url']
23            ?? 'https://api.plivo.com/v1/Account/' . $this->authId . '/Message/';
24    }
25
26    public function send(string $to, string $message): bool
27    {
28        try {
29            $client = \Config\Services::curlrequest();
30
31            $response = $client->post($this->apiUrl, [
32                'headers' => [
33                    // Plivo utilise HTTP Basic Auth
34                    'Authorization' => 'Basic ' . base64_encode($this->authId . ':' . $this->authToken),
35                    'Content-Type'  => 'application/json',
36                ],
37                'json' => [
38                    'src'  => $this->senderId,
39                    'dst'  => $to,
40                    'text' => $message,
41                ],
42            ]);
43
44            $status = $response->getStatusCode();
45
46            if ($status !== 202) {
47                log_message('error', '[PlivoSmsProvider] Échec envoi — HTTP ' . $status);
48                return false;
49            }
50
51            return true;
52
53        } catch (\Throwable $e) {
54            log_message('error', '[PlivoSmsProvider] ' . $e->getMessage());
55            return false;
56        }
57    }
58}