Completed
Push — master ( 6051f2...6377da )
by Dispositif
02:48
created

SMS::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
class SMS
13
{
14
    /**
15
     * @var \GuzzleHttp\Client
16
     */
17
    private $client;
18
19
    /**
20
     * SMS constructor.
21
     *
22
     * @param string $message
23
     *
24
     * @throws \Exception
25
     */
26
    public function __construct(?string $message = null)
27
    {
28
        $this->client = new \GuzzleHttp\Client();
29
        if (!getenv('FREE_SMS_URL')) {
30
            throw new \Exception('Pas d\'URL free mobile configurée');
31
        }
32
        if (!empty($message)) {
33
            $this->send($message);
34
        }
35
    }
36
37
    /**
38
     * @param string $message
39
     *
40
     * @return bool
41
     * @throws \Exception
42
     */
43
    public function send(string $message): bool
44
    {
45
        if (!getenv('FREE_SMS_URL')) {
46
            throw new \Exception('Pas d\'URL free mobile configurée');
47
        }
48
        $sender = getenv('BOT_NAME') ?? '';
49
        $message = sprintf('%s : %s', $sender, $message);
50
        $url = getenv('FREE_SMS_URL').urlencode($message);
51
52
        $response = $this->client->get($url, ['timeout' => 8]);
53
        if (200 === $response->getStatusCode()) {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
}
60