Sns   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A send() 0 12 3
A payload() 0 17 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 = collect();
51
        foreach ($this->recipients as $recipient) {
52
            $response->put(
53
                $recipient,
54
                $this->client->publish($this->payload($recipient))
55
            );
56
        }
57
58
        return (count($this->recipients) == 1) ? $response->first() : $response;
59
    }
60
61
    /**
62
     * @param string $recipient
63
     * @return array
64
     */
65
    protected function payload($recipient)
66
    {
67
        return [
68
            'Message' => $this->body,
69
            'MessageAttributes' => [
70
                'AWS.SNS.SMS.SenderID' => [
71
                    'DataType' => 'String',
72
                    'StringValue' => $this->settings->sender,
73
                ],
74
                'AWS.SNS.SMS.SMSType' => [
75
                    'DataType' => 'String',
76
                    'StringValue' => $this->settings->type,
77
                ],
78
            ],
79
            'PhoneNumber' => $recipient,
80
        ];
81
    }
82
}
83