refine test mode

This commit is contained in:
Maxime 2025-02-10 21:28:42 +01:00
parent 6ebae6201c
commit a076368f51
5 changed files with 27 additions and 10 deletions

View File

@ -2,5 +2,6 @@
return [
'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

@ -5,6 +5,7 @@ namespace Bluesquare\Push;
use Bluesquare\Push\PushTemplate;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
@ -17,9 +18,17 @@ class PushApi
*/
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->test_mode = $test_mode;
}
/**
@ -28,7 +37,7 @@ class PushApi
* @return \Psr\Http\Message\StreamInterface
* @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';
@ -51,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 = [];
foreach ($template as $key => $value) {

View File

@ -20,7 +20,7 @@ class PushChannel
* @return \Psr\Http\Message\StreamInterface
* @throws PushException
*/
public function send($notifiable, Notification $notification): StreamInterface
public function send($notifiable, Notification $notification): StreamInterface|false
{
$template = $notification->toTemplate($notifiable);

View File

@ -19,7 +19,7 @@ class PushServiceProvider extends ServiceProvider
);
$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']);
});
}

View File

@ -78,10 +78,6 @@ class PushTemplate implements \JsonSerializable
public function __construct(string $template_id)
{
$this->template_id = $template_id;
if (env('PUSH_TEST_MODE', false)) {
$this->testMode();
}
}
/**