Sms::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Odeen
5
 * Date: 2016/5/8
6
 * Time: 23:42.
7
 */
8
9
namespace Caikeal\LaravelSms\sms;
10
11
use Caikeal\LaravelSms\lib\Juhe;
12
use Caikeal\LaravelSms\lib\Rest;
13
14
class Sms
15
{
16
    protected $smsData = [
17
        'to'           => null,
18
        'templates'    => [],
19
        'templateData' => [],
20
        'content'      => null,
21
    ];
22
23
    protected $agent;
24
25
    protected $config;
26
27
    public function __construct($config)
28
    {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Get all the data of SMS/voice verify.
34
     *
35
     * @param null|string $name
36
     *
37
     * @return mixed
38
     */
39
    public function getData($name = null)
40
    {
41
        if (is_string($name) && isset($this->smsData["$name"])) {
42
            return $this->smsData[$name];
43
        }
44
45
        return $this->smsData;
46
    }
47
48
    /**
49
     * send mobile.
50
     *
51
     * @param $mobile
52
     *
53
     * @return $this
54
     */
55
    public function to($mobile)
56
    {
57
        $this->smsData['to'] = $mobile;
58
59
        return $this;
60
    }
61
62
    /**
63
     * send content for part of sms-providers support content-sms.
64
     *
65
     * @param $content
66
     *
67
     * @return $this
68
     */
69
    public function content($content)
70
    {
71
        $this->smsData['content'] = $content;
72
73
        return $this;
74
    }
75
76
    /**
77
     * set template-id for part of sms-providers support template-sms.
78
     *
79
     * @param $agentName
80
     * @param null $tempId
81
     *
82
     * @return $this
83
     */
84
    public function template($agentName = 'SMS_AGENT', $tempId = null)
85
    {
86
        if ($agentName === 'SMS_AGENT') {
87
            $agentName = $this->config;
88
        }
89
        if (is_array($agentName)) {
90
            foreach ($agentName as $k => $v) {
91
                $this->template($k, $v);
92
            }
93
        } elseif ($agentName && $tempId) {
94
            if (!isset($this->smsData['templates']) || !is_array($this->smsData['templates'])) {
95
                $this->smsData['templates'] = [];
96
            }
97
            $this->smsData['templates']["$agentName"] = $tempId;
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * set template-data for part of sms-providers support template-sms.
105
     *
106
     * @param array $data
107
     *
108
     * @return $this
109
     */
110
    public function data(array $data)
111
    {
112
        $this->smsData['templateData'] = $data;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @throws \Exception
119
     *
120
     * @return mixed
121
     */
122
    public function send()
123
    {
124
        if ($this->config === 'YunTongXun') {
125
            $rest = new Rest(config('sms.agents.'.$this->config));
126
127
            return $rest->sendTemplateSMS($this->smsData['to'], $this->smsData['templateData'], $this->smsData['templates']['YunTongXun']);
128
        } elseif ($this->config === 'Juhe') {
129
            $rest = new Juhe(config('sms.agents.'.$this->config));
130
131
            return $rest->sendTemplateSMS($this->smsData['to'], $this->smsData['templateData'], $this->smsData['templates']['Juhe']);
132
        } else {
133
            throw new \Exception('make sure you have choose a right agent');
134
        }
135
    }
136
}
137