From ab38c7a932aa3e7a41bf87d3aa23554dfc02d264 Mon Sep 17 00:00:00 2001 From: Erlang Parasu Date: Sat, 29 Feb 2020 17:03:48 +0700 Subject: [PATCH] testable class --- src/Fcm.php | 19 ++++++++++++------- src/FcmServiceProvider.php | 4 +++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Fcm.php b/src/Fcm.php index 7185fc0..845716a 100644 --- a/src/Fcm.php +++ b/src/Fcm.php @@ -8,6 +8,8 @@ */ class Fcm { + const ENDPOINT = 'https://fcm.googleapis.com/fcm/send'; + protected $recipients; protected $topic; protected $data; @@ -15,6 +17,13 @@ class Fcm protected $timeToLive; protected $priority; + protected $serverKey; + + public function __construct($serverKey) + { + $this->serverKey = $serverKey; + } + public function to(array $recipients) { $this->recipients = $recipients; @@ -66,8 +75,6 @@ public function timeToLive(int $timeToLive) public function send() { - $fcmEndpoint = 'https://fcm.googleapis.com/fcm/send'; - $payloads = [ 'content_available' => true, 'priority' => $this->priority ?? 'high', @@ -85,15 +92,13 @@ public function send() $payloads['time_to_live'] = (int) $this->timeToLive; } - $serverKey = config('laravel-fcm.server_key'); - $headers = [ - 'Authorization: key=' . $serverKey, - 'Content-Type: application/json' + 'Authorization: key=' . $this->serverKey, + 'Content-Type: application/json', ]; $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $fcmEndpoint); + curl_setopt($ch, CURLOPT_URL, self::ENDPOINT); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); diff --git a/src/FcmServiceProvider.php b/src/FcmServiceProvider.php index 0981ec6..6525bd2 100644 --- a/src/FcmServiceProvider.php +++ b/src/FcmServiceProvider.php @@ -21,7 +21,9 @@ public function boot() public function register() { $this->app->bind('fcm', function ($app) { - return new Fcm(); + return new Fcm( + config('laravel-fcm.server_key') + ); }); } }