Verified Commit 12fb7afa authored by Richard Weinhold's avatar Richard Weinhold 🔨
Browse files

fixes minor issues

parent 67320932
......@@ -10,7 +10,7 @@ use RuntimeException;
class APNS extends Handler
{
private const URLS = [
Config::ENV_DEVELOPMENT => 'https://api.sandbox.push.apple.com:443/3/device',
Config::ENV_DEVELOPMENT => 'https://api.development.push.apple.com:443/3/device',
Config::ENV_PRODUCTION => 'https://api.push.apple.com:443/3/device',
];
......@@ -79,7 +79,7 @@ class APNS extends Handler
return [];
}
$payload = array_merge([
$payload = array_merge_recursive([
'aps' => [
'alert' => $message->getTitle() !== null ? ['title' => $message->getTitle(), 'body' => $message->getBody()] : $message->getBody(),
'badge' => $message->getBadge(),
......@@ -105,7 +105,6 @@ class APNS extends Handler
];
$options = [
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_PORT => $this->port,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
......@@ -119,47 +118,58 @@ class APNS extends Handler
$options[CURLOPT_KEYPASSWD] = $this->certPassphrase;
}
$feedback = [];
$curl = curl_init();
try {
foreach ($this->devices as $deviceToken) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
// cleanup device tokens
$deviceToken = str_replace(' ', '', trim($deviceToken, '<> '));
$options[CURLOPT_URL] = "{$this->endpoint}/{$deviceToken}";
$feedback = [];
foreach ($this->devices as $deviceKey => $deviceToken) {
// setup curl for device push notification
curl_setopt_array($curl, $options);
// cleanup device tokens
$deviceToken = str_replace(' ', '', trim($deviceToken, '<> '));
$options[CURLOPT_URL] = "{$this->endpoint}/{$deviceToken}";
// execute request
$result = curl_exec($curl);
$httpStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// setup curl for device push notification
curl_setopt_array($curl, $options);
if ($result === false) {
$feedback[$deviceToken] = new RuntimeException("Request failed with.", 500);
continue;
}
// execute request
$result = curl_exec($curl);
if (200 !== $httpStatusCode) {
$errorCode = curl_errno($curl);
$error = curl_error($curl);
$result = @json_decode($result, true);
if (isset($result['reason'])) {
$feedback[$deviceToken] = new RuntimeException("Request failed with: [{$errorCode}]: {$error} - Reason: {$result['reason']}", $httpStatusCode);
} else {
$feedback[$deviceToken] = new RuntimeException("Request failed with: [{$errorCode}]: {$error}", $httpStatusCode);
}
continue;
}
$httpStatusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
// success!
if ($result !== false && $httpStatusCode === 200) {
unset($this->devices[$deviceKey]);
$feedback[$deviceToken] = null;
continue;
}
return $feedback;
} finally {
curl_close($curl);
// error handling
$errorCode = curl_errno($curl);
$error = curl_error($curl);
if ($result === false) {
if (!empty($error)) {
$feedback[$deviceToken] = new RuntimeException("Request failed with: [{$errorCode}]: {$error}", 500);
} elseif ($httpStatusCode !== 0) {
$feedback[$deviceToken] = new RuntimeException("Request failed with: HTTP status code {$httpStatusCode}.", 500);
} else {
$feedback[$deviceToken] = new RuntimeException("Request failed.", 500);
}
continue;
}
$result = @json_decode($result, true);
if (isset($result['reason'])) {
$feedback[$deviceToken] = new RuntimeException("Request failed with: [{$errorCode}]: {$error} - Reason: {$result['reason']}", $httpStatusCode);
} else {
$feedback[$deviceToken] = new RuntimeException("Request failed with: [{$errorCode}]: {$error}", $httpStatusCode);
}
}
curl_close($curl);
$this->devices = [];
return $feedback;
}
}
......@@ -26,7 +26,7 @@ class FCM extends Handler
*/
private $timeout;
public function __construct(string $token, string $url = self::FCM_ENDPOINT, int $timeout = 10)
public function __construct(string $token, string $url = self::FCM_ENDPOINT, int $timeout = 30)
{
$this->endpoint = $url;
$this->token = $token;
......@@ -77,7 +77,6 @@ class FCM extends Handler
$content = json_encode($payload, JSON_UNESCAPED_UNICODE);
$headers = [
"Authorization: key={$this->token}",
"Content-Type: application/json",
......@@ -99,7 +98,7 @@ class FCM extends Handler
// execute request
$result = curl_exec($curl);
$httpStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$httpStatusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
if ($result === false || 200 !== $httpStatusCode) {
$errorCode = curl_errno($curl);
......@@ -112,6 +111,7 @@ class FCM extends Handler
return [new RuntimeException("Requests was send, but resulted in an error.", 400)];
}
$this->devices = [];
return [null];
} finally {
curl_close($curl);
......
......@@ -18,7 +18,7 @@ class PushNotification
* PushNotification constructor.
* @param array<string, Handler> $handlers
*/
public function __construct(array $handlers)
public function __construct(array $handlers = [])
{
foreach ($handlers as $handler) {
if (!$handler instanceof Handler) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment