Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TwilioWhatsAppProvider
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
30
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 / 24
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2// App/Services/Communication/Providers/WhatsApp/TwilioWhatsAppProvider.php
3
4namespace App\Services\Communication\Providers\WhatsApp;
5
6use App\Services\Communication\Contracts\WhatsAppProviderInterface;
7
8class TwilioWhatsAppProvider implements WhatsAppProviderInterface
9{
10    private string $apiUrl;
11    private string $accountSid;
12    private string $authToken;
13    private string $from;
14
15    public function __construct()
16    {
17        $config = config('CommunicationProviders');
18
19        $this->accountSid = $config->twilio_whatsapp['account_sid'] ?? '';
20        $this->authToken  = $config->twilio_whatsapp['auth_token']  ?? '';
21        $this->from       = $config->twilio_whatsapp['from']        ?? '';
22        $this->apiUrl     = $config->twilio_whatsapp['api_url']
23            ?? 'https://api.twilio.com/2010-04-01/Accounts/' . $this->accountSid . '/Messages.json';
24    }
25
26    public function send(string $to, string $message): bool
27    {
28        try {
29            $client = \Config\Services::curlrequest();
30
31            // Twilio WhatsApp — préfixe whatsapp: obligatoire
32            $response = $client->post($this->apiUrl, [
33                'headers' => [
34                    // Basic Auth : AccountSid:AuthToken
35                    'Authorization' => 'Basic ' . base64_encode($this->accountSid . ':' . $this->authToken),
36                    'Content-Type'  => 'application/x-www-form-urlencoded',
37                ],
38                'form_params' => [
39                    'From' => 'whatsapp:' . $this->from,
40                    'To'   => 'whatsapp:' . $to,
41                    'Body' => $message,
42                ],
43            ]);
44
45            $status = $response->getStatusCode();
46
47            if ($status !== 201) {
48                log_message('error', '[TwilioWhatsAppProvider] Échec envoi — HTTP ' . $status);
49                return false;
50            }
51
52            $body = json_decode($response->getBody(), true);
53
54            // Twilio : status = queued | sent | delivered | failed
55            if (in_array($body['status'] ?? '', ['failed', 'undelivered'])) {
56                log_message('error', '[TwilioWhatsAppProvider] Statut inattendu : ' . ($body['status'] ?? '') . ' — ' . ($body['error_message'] ?? ''));
57                return false;
58            }
59
60            return true;
61
62        } catch (\Throwable $e) {
63            log_message('error', '[TwilioWhatsAppProvider] ' . $e->getMessage());
64            return false;
65        }
66    }
67}