Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
|
a076368f51 | |
|
6ebae6201c |
|
@ -2,5 +2,6 @@
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'api_key' => env('PUSH_API_KEY', null),
|
'api_key' => env('PUSH_API_KEY', null),
|
||||||
'api_url' => env('PUSH_API_URL', null)
|
'api_url' => env('PUSH_API_URL', null),
|
||||||
|
'test_mode' => env('PUSH_TEST_MODE', false),
|
||||||
];
|
];
|
||||||
|
|
|
@ -4,6 +4,8 @@ namespace Bluesquare\Push;
|
||||||
|
|
||||||
use Bluesquare\Push\PushTemplate;
|
use Bluesquare\Push\PushTemplate;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Psr\Http\Message\StreamInterface;
|
use Psr\Http\Message\StreamInterface;
|
||||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
|
||||||
|
@ -16,9 +18,17 @@ class PushApi
|
||||||
*/
|
*/
|
||||||
private $api_key;
|
private $api_key;
|
||||||
|
|
||||||
public function __construct(string $api_key)
|
/**
|
||||||
|
* Test mode
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $test_mode;
|
||||||
|
|
||||||
|
public function __construct(string $api_key, bool $test_mode = false)
|
||||||
{
|
{
|
||||||
$this->api_key = $api_key;
|
$this->api_key = $api_key;
|
||||||
|
$this->test_mode = $test_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +37,7 @@ class PushApi
|
||||||
* @return \Psr\Http\Message\StreamInterface
|
* @return \Psr\Http\Message\StreamInterface
|
||||||
* @throws PushException
|
* @throws PushException
|
||||||
*/
|
*/
|
||||||
public function send($template, $notifiable): StreamInterface
|
public function send($template, $notifiable): StreamInterface|false
|
||||||
{
|
{
|
||||||
$url = config('push.api_url') ?? 'https://push.bluesquare.io/api/send';
|
$url = config('push.api_url') ?? 'https://push.bluesquare.io/api/send';
|
||||||
|
|
||||||
|
@ -50,6 +60,17 @@ class PushApi
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($this->test_mode) {
|
||||||
|
if (! empty($this->api_key)) {
|
||||||
|
$template->testMode();
|
||||||
|
} else {
|
||||||
|
Log::debug("Push test mode: API key missing, discarding notification", $template->toArray());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} elseif (empty($this->api_key)) {
|
||||||
|
throw new PushException('Push API key is required');
|
||||||
|
}
|
||||||
|
|
||||||
$multipart_form = [];
|
$multipart_form = [];
|
||||||
|
|
||||||
foreach ($template as $key => $value) {
|
foreach ($template as $key => $value) {
|
||||||
|
@ -73,25 +94,24 @@ class PushApi
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($value as $subkey => $subvalue) {
|
$values = Arr::dot($value);
|
||||||
|
|
||||||
|
foreach ($values as $subkey => $subvalue) {
|
||||||
|
$subkey = str_replace('.', '][', $subkey);
|
||||||
|
|
||||||
$multipart_form[] = [
|
$multipart_form[] = [
|
||||||
'name' => "{$key}[$subkey]" . (is_array($subvalue) ? '[' . key($subvalue) . ']' : '' ),
|
'name' => "{$key}[$subkey]",
|
||||||
'contents' => (is_array($subvalue) ? reset($subvalue) : $subvalue)
|
'contents' => is_array($subvalue) ? json_encode($subvalue) : $subvalue
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return $client->request('post', $url, [
|
||||||
return $client->request('post', $url, [
|
'multipart' => $multipart_form,
|
||||||
'multipart' => $multipart_form,
|
'headers' => [
|
||||||
'headers' => [
|
'Authorization' => 'Bearer ' . $this->api_key,
|
||||||
'Authorization' => 'Bearer ' . $this->api_key,
|
'Accept' => 'application/json'
|
||||||
'Accept' => 'application/json'
|
]
|
||||||
]
|
])->getBody();
|
||||||
])->getBody();
|
|
||||||
|
|
||||||
} catch(\Exception $e) {
|
|
||||||
throw new PushException($e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ class PushChannel
|
||||||
* @return \Psr\Http\Message\StreamInterface
|
* @return \Psr\Http\Message\StreamInterface
|
||||||
* @throws PushException
|
* @throws PushException
|
||||||
*/
|
*/
|
||||||
public function send($notifiable, Notification $notification): StreamInterface
|
public function send($notifiable, Notification $notification): StreamInterface|false
|
||||||
{
|
{
|
||||||
$template = $notification->toTemplate($notifiable);
|
$template = $notification->toTemplate($notifiable);
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ class PushServiceProvider extends ServiceProvider
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->app->singleton(PushApi::class, function ($app) {
|
$this->app->singleton(PushApi::class, function ($app) {
|
||||||
return new PushApi($app['config']['push']['api_key']);
|
return new PushApi($app['config']['push']['api_key'], $app['config']['push']['test_mode']);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue