Completed
Push — master ( bac835...b34d26 )
by Kazi Mainuddin
12:13
created

Sns::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Tzsk\Sms\Drivers;
4
5
use Aws\Sns\SnsClient;
6
use Tzsk\Sms\Abstracts\Driver;
7
8
class Sns extends Driver
9
{
10
    /**
11
     * SNS Settings.
12
     *
13
     * @var object
14
     */
15
    protected $settings;
16
17
    /**
18
     * SNS Client.
19
     *
20
     * @var SnsClient
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 = SnsClient::factory([
0 ignored issues
show
Deprecated Code introduced by
The method Aws\AwsClient::factory() has been deprecated.

This method has been deprecated.

Loading history...
34
            'credentials' => [
35
                'key' => $this->settings->key,
36
                'secret' => $this->settings->secret,
37
            ],
38
            'region' => $this->settings->region,
39
            'version' => '2010-03-31',
40
        ]);
41
    }
42
43
    /**
44
     * Send text message and return response.
45
     *
46
     * @return object
47
     */
48
    public function send()
49
    {
50
        $response = ['status' => true, 'data' => []];
51
        foreach ($this->recipients as $recipient) {
52
            $response = ['status' => true];
53
            try {
54
                $response['data'] = $this->client->publish($this->payload($recipient));
55
            } catch (\Exception $e) {
56
                $response['status'] = false;
57
                $response['message'] = $e->getMessage();
58
            }
59
60
            $response['data'][$recipient] = $response;
61
        }
62
63
        return (object) $response;
64
    }
65
66
    /**
67
     * @param string $recipient
68
     * @return array
69
     */
70
    protected function payload($recipient)
71
    {
72
        return [
73
            'Message' => $this->body,
74
            'MessageAttributes' => [
75
                'AWS.SNS.SMS.SenderID' => [
76
                    'DataType' => 'String',
77
                    'StringValue' => $this->settings->sender,
78
                ],
79
                'AWS.SNS.SMS.SMSType' => [
80
                    'DataType' => 'String',
81
                    'StringValue' => $this->settings->type,
82
                ],
83
            ],
84
            'PhoneNumber' => $recipient,
85
        ];
86
    }
87
}
88