1 | <?php |
||
2 | /* |
||
3 | * This file is part of dispositif/wikibot application (@github) |
||
4 | * 2019-2023 © Philippe M./Irønie <[email protected]> |
||
5 | * For the full copyright and MIT license information, view the license file. |
||
6 | */ |
||
7 | |||
8 | declare(strict_types=1); |
||
9 | |||
10 | namespace App\Infrastructure; |
||
11 | |||
12 | use App\Application\InfrastructurePorts\SMSInterface; |
||
13 | use Exception; |
||
14 | use GuzzleHttp\Client; |
||
15 | |||
16 | class SMS implements SMSInterface |
||
17 | { |
||
18 | private readonly Client $client; |
||
19 | |||
20 | /** |
||
21 | * @throws Exception |
||
22 | */ |
||
23 | public function __construct(?string $message = null) |
||
24 | { |
||
25 | $this->client = new Client(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
26 | if (!getenv('FREE_SMS_URL')) { |
||
27 | throw new Exception('Pas d\'URL free mobile configurée'); |
||
28 | } |
||
29 | if (!empty($message)) { |
||
30 | $this->send($message); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @throws Exception |
||
36 | */ |
||
37 | public function send(string $message): bool |
||
38 | { |
||
39 | $sender = getenv('BOT_NAME') ?? ''; |
||
40 | $message = sprintf('%s : %s', $sender, $message); |
||
41 | $url = getenv('FREE_SMS_URL') . urlencode($message); |
||
42 | |||
43 | $response = $this->client->get($url, ['timeout' => 120]); |
||
44 | |||
45 | return 200 === $response->getStatusCode(); |
||
46 | } |
||
47 | } |
||
48 |