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