1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LBHurtado\EngageSpark; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
7
|
|
|
|
8
|
|
|
class EngageSpark |
9
|
|
|
{ |
10
|
|
|
/** @var HttpClient */ |
11
|
|
|
protected $client; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
protected $end_points; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $api_key; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $org_id; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $sender_id; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* EngageSpark constructor. |
27
|
|
|
* @param HttpClient $client |
28
|
|
|
*/ |
29
|
|
|
public function __construct(HttpClient $client) |
30
|
|
|
{ |
31
|
|
|
$this->client = $client; |
32
|
|
|
$this->initializeProperties(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set protected and public properties. |
37
|
|
|
*/ |
38
|
|
|
protected function initializeProperties(): void |
39
|
|
|
{ |
40
|
|
|
tap(config('engagespark'), function($config) { |
41
|
|
|
$this->end_points = Arr::get($config, 'end_points'); |
|
|
|
|
42
|
|
|
$this->api_key = Arr::get($config, 'api_key'); |
43
|
|
|
$this->org_id = Arr::get($config, 'org_id'); |
44
|
|
|
$this->sender_id = Arr::get($config, 'sender_id'); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $params |
50
|
|
|
* @param string $mode |
51
|
|
|
* @return mixed |
52
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
53
|
|
|
*/ |
54
|
|
|
public function send($params, $mode = 'sms') |
55
|
|
|
{ |
56
|
|
|
$endPoint = $this->getEndPoint($mode); |
57
|
|
|
$request = $this->getFormattedRequestHeadersAndJsonData($params); |
58
|
|
|
|
59
|
|
|
return optional($this->client->request('POST', $endPoint, $request), function ($response) { |
60
|
|
|
return $this->getFormattedResponse($response); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getOrgId() |
68
|
|
|
{ |
69
|
|
|
return $this->org_id; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getSenderId() |
76
|
|
|
{ |
77
|
|
|
return $this->sender_id; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $mode |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function getEndPoint($mode) |
85
|
|
|
{ |
86
|
|
|
return Arr::get($this->end_points, $mode, $this->end_points['sms']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function getApiKey() |
93
|
|
|
{ |
94
|
|
|
return $this->api_key; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function getHeaders() |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
'Authorization' => "Token {$this->getApiKey()}", |
104
|
|
|
'Accept' => 'application/json', |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* There may be base parameters in the future. Just putting it here. |
110
|
|
|
* |
111
|
|
|
* @param array $params |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
protected function consolidateParams(array $params) |
115
|
|
|
{ |
116
|
|
|
$base = []; |
117
|
|
|
|
118
|
|
|
return \array_merge($base, \array_filter($params)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $params |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
protected function getFormattedRequestHeadersAndJsonData($params): array |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
'headers' => $this->getHeaders(), |
129
|
|
|
'json' => $this->consolidateParams($params) |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
135
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
136
|
|
|
*/ |
137
|
|
|
protected function getFormattedResponse(\Psr\Http\Message\ResponseInterface $response) |
138
|
|
|
{ |
139
|
|
|
$response = \json_decode((string)$response->getBody(), true); |
140
|
|
|
return $response; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..