Farazsms::payload()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Tzsk\Sms\Drivers;
4
5
use GuzzleHttp\Client;
6
use Tzsk\Sms\Abstracts\Driver;
7
8
class Farazsms extends Driver
9
{
10
    /**
11
     * Farazsms Settings.
12
     *
13
     * @var null|object
14
     */
15
    protected $settings;
16
17
    /**
18
     * Farazsms Client.
19
     *
20
     * @var null|Client
21
     */
22
    protected $client;
23
24
    /**
25
     * Construct the class with the relevant settings.
26
     *
27
     * SendSmsInterface constructor.
28
     * @param $settings object
29
     */
30
    public function __construct($settings)
31
    {
32
        $this->settings = (object) $settings;
33
        $this->client = new Client();
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
46
        foreach ($this->recipients as $recipient) {
47
            $result = $this->client->request(
48
                'POST',
49
                $this->settings->url,
50
                $this->payload($recipient)
51
            );
52
            $response->put($recipient, json_decode($result->getBody()));
53
        }
54
55
        return (count($this->recipients) == 1) ? $response->first() : $response;
56
    }
57
58
    /**
59
     * @param string $recipient
60
     * @return array
61
     */
62
    protected function payload($recipient)
63
    {
64
        return [
65
            'form_params' => [
66
                'uname' => $this->settings->username,
67
                'pass' => $this->settings->password,
68
                'from' => $this->settings->from,
69
                'message' => $this->body,
70
                'to' => json_encode([$recipient]),
71
                'op'=>'send'
72
            ],
73
        ];
74
    }
75
}
76