diff --git a/config/push.php b/config/push.php index 64d8f31..b2d9aa4 100644 --- a/config/push.php +++ b/config/push.php @@ -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), ]; diff --git a/src/PushApi.php b/src/PushApi.php index 1de2fa1..265b737 100644 --- a/src/PushApi.php +++ b/src/PushApi.php @@ -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) { @@ -85,7 +105,7 @@ class PushApi ]; } } - + return $client->request('post', $url, [ 'multipart' => $multipart_form, 'headers' => [ diff --git a/src/PushChannel.php b/src/PushChannel.php index 96558c7..b22a0f8 100644 --- a/src/PushChannel.php +++ b/src/PushChannel.php @@ -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); diff --git a/src/PushServiceProvider.php b/src/PushServiceProvider.php index c544261..80b29a7 100644 --- a/src/PushServiceProvider.php +++ b/src/PushServiceProvider.php @@ -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']); }); } diff --git a/src/PushTemplate.php b/src/PushTemplate.php index f80eb2c..ed98279 100644 --- a/src/PushTemplate.php +++ b/src/PushTemplate.php @@ -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(); - } } /**