1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StephaneCoinon\SendGridActivity; |
4
|
|
|
|
5
|
|
|
use Http\Client\Common\Plugin\BaseUriPlugin; |
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
8
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
9
|
|
|
use StephaneCoinon\SendGridActivity\HttpClientFactory; |
10
|
|
|
use StephaneCoinon\SendGridActivity\Requests\Request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SendGrid API client. |
14
|
|
|
*/ |
15
|
|
|
class SendGrid |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* API base URL. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $apiUrl = 'https://api.sendgrid.com'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* API version. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $apiVersion = 'v3'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Underlying HTTP client. |
33
|
|
|
* |
34
|
|
|
* @var \Http\Client\HttpClient |
35
|
|
|
*/ |
36
|
|
|
protected $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Message factory. |
40
|
|
|
* |
41
|
|
|
* @var \Http\Message\MessageFactory |
42
|
|
|
*/ |
43
|
|
|
protected $messageFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Make a new ApiClient instance. |
47
|
|
|
* |
48
|
|
|
* If $client is null, a new HttpClient using $apiKey is instantiated. |
49
|
|
|
* If $apiKey is null, SENDGRID_API_KEY environment variable is used. |
50
|
|
|
* $apiKey is not used when $client is specified. |
51
|
|
|
* |
52
|
|
|
* @param null|string $apiKey |
53
|
|
|
* @param null|\Http\Client\HttpClient $client |
54
|
|
|
*/ |
55
|
|
|
public function __construct($apiKey = null, HttpClient $client = null) |
56
|
|
|
{ |
57
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
58
|
|
|
$this->client = $client ?? HttpClientFactory::create( |
59
|
|
|
$apiKey ?? getenv('SENDGRID_API_KEY'), |
60
|
|
|
[ |
|
|
|
|
61
|
|
|
new BaseUriPlugin( |
62
|
|
|
UriFactoryDiscovery::find()->createUri($this->apiUrl) |
63
|
|
|
) |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Static constructor to get a ApiClient instance with a given HTTP client. |
70
|
|
|
* |
71
|
|
|
* @param \Http\Client\HttpClient $client |
72
|
|
|
* @return self |
73
|
|
|
*/ |
74
|
|
|
public static function newWithClient(HttpClient $client) |
75
|
|
|
{ |
76
|
|
|
return new static(null, $client); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get underlying HTTP client. |
81
|
|
|
* |
82
|
|
|
* @return \Http\Client\HttpClient |
83
|
|
|
*/ |
84
|
|
|
public function getClient(): HttpClient |
85
|
|
|
{ |
86
|
|
|
return $this->client; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Make a "raw" HTTP request. |
91
|
|
|
* |
92
|
|
|
* JSON responses are automatically decoded to an array. |
93
|
|
|
* |
94
|
|
|
* @param string $method HTTP method |
95
|
|
|
* @param string $url |
96
|
|
|
* @return array|string |
97
|
|
|
*/ |
98
|
|
|
public function requestRaw(string $method, string $url = '') |
99
|
|
|
{ |
100
|
|
|
$response = $this->client->sendRequest( |
101
|
|
|
$this->messageFactory->createRequest( |
102
|
|
|
$method, "{$this->apiVersion}/{$url}" |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if ($response->getStatusCode() != 200) { |
107
|
|
|
var_dump(['request failed' => $response->getBody()->getContents()]); die(); |
|
|
|
|
108
|
|
|
// throw new \Exception('Request failed'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$content = $response->getBody()->getContents(); |
112
|
|
|
$contentType = $response->getHeader('Content-Type'); |
113
|
|
|
$isJson = in_array('application/json', $contentType); |
114
|
|
|
|
115
|
|
|
return $isJson ? json_decode($content, true) : $content; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Make a request using a Request instance. |
120
|
|
|
* |
121
|
|
|
* @param Request $request |
122
|
|
|
* @return Response|Response[] |
123
|
|
|
*/ |
124
|
|
|
public function request(Request $request) |
125
|
|
|
{ |
126
|
|
|
$responseModel = $request->getResponseModel(); |
127
|
|
|
$response = $this->requestRaw( |
128
|
|
|
$request->getMethod(), $request->buildUrl() |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $responseModel::createFromApiResponse($response); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: