Farazsms   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 15 3
A payload() 0 13 1
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