Completed
Pull Request — master (#22)
by mahdi
01:31
created

Tsms::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
namespace Tzsk\Sms\Drivers;
4
5
use Tzsk\Sms\Abstracts\Driver;
6
use \SoapClient;
7
8
class Tsms extends Driver
9
{
10
    /**
11
     * Tsms Settings.
12
     *
13
     * @var null|object
14
     */
15
    protected $settings;
16
17
    /**
18
     * Smsir Client.
19
     *
20
     * @var null|SoapClient
21
     */
22
    protected $client;
23
24
    /**
25
     * Construct the class with the relevant settings.
26
     *
27
     * @param $settings
28
     * @throws \SoapFault
29
     */
30
    public function __construct($settings)
31
    {
32
        $this->settings = (object) $settings;
33
        $this->client = new SoapClient($this->settings->url);
34
    }
35
36
    /**
37
     * Send text message and return response.
38
     *
39
     * @return object
40
     * @throws \GuzzleHttp\Exception\GuzzleException
41
     */
42
    public function send()
43
    {
44
        $response = collect();
45
        foreach ($this->recipients as $recipient) {
46
            $result = $this
47
                ->client
48
                ->sendSms(
49
                    $this->settings->username,
50
                    $this->settings->password,
51
                    [$this->settings->from],
52
                    [$recipient],
53
                    [$this->body],
54
                    [],
55
                    rand()
56
                );
57
58
            $response->put($recipient, $result);
59
        }
60
61
        return (count($this->recipients) == 1) ? $response->first() : $response;
62
    }
63
}
64