Compare commits

..

No commits in common. "main" and "0.3" have entirely different histories.
main ... 0.3

4 changed files with 20 additions and 41 deletions

View File

@ -2,6 +2,5 @@
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),
]; ];

View File

@ -4,8 +4,6 @@ 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;
@ -18,17 +16,9 @@ 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;
} }
/** /**
@ -37,7 +27,7 @@ class PushApi
* @return \Psr\Http\Message\StreamInterface * @return \Psr\Http\Message\StreamInterface
* @throws PushException * @throws PushException
*/ */
public function send($template, $notifiable): StreamInterface|false public function send($template, $notifiable): StreamInterface
{ {
$url = config('push.api_url') ?? 'https://push.bluesquare.io/api/send'; $url = config('push.api_url') ?? 'https://push.bluesquare.io/api/send';
@ -60,17 +50,6 @@ 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) {
@ -94,18 +73,15 @@ class PushApi
continue; continue;
} }
$values = Arr::dot($value); foreach ($value as $subkey => $subvalue) {
foreach ($values as $subkey => $subvalue) {
$subkey = str_replace('.', '][', $subkey);
$multipart_form[] = [ $multipart_form[] = [
'name' => "{$key}[$subkey]", 'name' => "{$key}[$subkey]" . (is_array($subvalue) ? '[' . key($subvalue) . ']' : '' ),
'contents' => is_array($subvalue) ? json_encode($subvalue) : $subvalue 'contents' => (is_array($subvalue) ? reset($subvalue) : $subvalue)
]; ];
} }
} }
try {
return $client->request('post', $url, [ return $client->request('post', $url, [
'multipart' => $multipart_form, 'multipart' => $multipart_form,
'headers' => [ 'headers' => [
@ -113,5 +89,9 @@ class PushApi
'Accept' => 'application/json' 'Accept' => 'application/json'
] ]
])->getBody(); ])->getBody();
} catch(\Exception $e) {
throw new PushException($e->getMessage());
}
} }
} }

View File

@ -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|false public function send($notifiable, Notification $notification): StreamInterface
{ {
$template = $notification->toTemplate($notifiable); $template = $notification->toTemplate($notifiable);

View File

@ -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'], $app['config']['push']['test_mode']); return new PushApi($app['config']['push']['api_key']);
}); });
} }