TheGarious /
firebase-service-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Firebase Service bundle. |
||
| 5 | * |
||
| 6 | * (c) Gary Houbre <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Ghoubre\FirebaseServiceBundle\Service; |
||
| 13 | |||
| 14 | use Exception; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Class FirebaseService |
||
| 18 | * |
||
| 19 | * @author Gary HOUBRE <[email protected]> |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | class FirebaseNotificationService |
||
| 23 | { |
||
| 24 | protected $server_key; |
||
| 25 | |||
| 26 | protected $content_available; |
||
| 27 | |||
| 28 | protected $time_to_live; |
||
| 29 | |||
| 30 | protected $message; |
||
| 31 | |||
| 32 | const FCM_URL = "https://fcm.googleapis.com/fcm/send"; |
||
| 33 | |||
| 34 | 8 | public function __construct($server_key, $content_available = true, $time_to_live = 30) |
|
| 35 | { |
||
| 36 | 8 | $this->server_key = $server_key; |
|
| 37 | 8 | $this->content_available = $content_available; |
|
| 38 | 8 | $this->time_to_live = $time_to_live; |
|
| 39 | 8 | } |
|
| 40 | |||
| 41 | 3 | public function createMessage($message) |
|
| 42 | { |
||
| 43 | 3 | if (!array_key_exists("to", $message)) { |
|
| 44 | 1 | return false; |
|
| 45 | } |
||
| 46 | 2 | $this->message["to"] = $message["to"]; |
|
| 47 | |||
| 48 | |||
| 49 | 2 | if (array_key_exists("title", $message)) { |
|
| 50 | $this->message["notification"]["title"] = $message["title"]; |
||
| 51 | } |
||
| 52 | 2 | if (array_key_exists("body", $message)) { |
|
| 53 | $this->message["notification"]["body"] = $message["body"]; |
||
| 54 | } |
||
| 55 | 2 | if (array_key_exists("badge", $message)) { |
|
| 56 | $this->message["notification"]["badge"] = $message["badge"]; |
||
| 57 | } |
||
| 58 | 2 | if (array_key_exists("sound", $message)) { |
|
| 59 | $this->message["notification"]["sound"] = $message["sound"]; |
||
| 60 | } |
||
| 61 | 2 | if (array_key_exists("data", $message)) { |
|
| 62 | $this->message["data"] = $message["data"]; |
||
| 63 | } |
||
| 64 | |||
| 65 | 2 | $this->message["content_available"] = true; |
|
| 66 | |||
| 67 | 2 | $this->message["time_to_live"] = 30; |
|
| 68 | |||
| 69 | 2 | return true; |
|
| 70 | } |
||
| 71 | |||
| 72 | 1 | public function sendNotification() |
|
| 73 | { |
||
| 74 | 1 | $content = json_encode($this->message); |
|
| 75 | |||
| 76 | 1 | $curl = curl_init(self::FCM_URL); |
|
| 77 | 1 | curl_setopt($curl, CURLOPT_HEADER, false); |
|
| 78 | 1 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 79 | 1 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
|
| 80 | 1 | "Content-type: application/json", |
|
| 81 | 1 | "Authorization: key=".$this->server_key |
|
| 82 | ) |
||
| 83 | ); |
||
| 84 | 1 | $curlConf["header"] = curl_getinfo($curl, CURLOPT_HEADER); |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 85 | |||
| 86 | 1 | curl_setopt($curl, CURLOPT_POST, true); |
|
| 87 | 1 | curl_setopt($curl, CURLOPT_POSTFIELDS, $content); |
|
| 88 | // @codeCoverageIgnoreStart |
||
| 89 | $json_response = curl_exec($curl); |
||
| 90 | $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
| 91 | // @codeCoverageIgnoreEnd |
||
| 92 | 1 | curl_close($curl); |
|
| 93 | |||
| 94 | return [ |
||
| 95 | 1 | 'curlConf' => $curlConf, |
|
| 96 | 1 | 'status' => $status, |
|
| 97 | 1 | 'response' => $json_response |
|
| 98 | ]; |
||
| 99 | } |
||
| 100 | |||
| 101 | 5 | public function parseResult(array $result) |
|
| 102 | { |
||
| 103 | 5 | if ($result["status"] == 200) { |
|
| 104 | 1 | return json_decode($result["response"], true); |
|
| 105 | 4 | } elseif ($result["status"] == 400) { |
|
| 106 | 1 | throw new Exception("Invalid fields or request not parsed as JSON"); |
|
| 107 | 3 | } elseif ($result["status"] == 401) { |
|
| 108 | 1 | throw new Exception("Authenticate Error"); |
|
| 109 | 2 | } elseif ($result["status"] >= 500 && $result["status"] < 600) { |
|
| 110 | 1 | throw new Exception("Internal error in the FCM connection server"); |
|
| 111 | } else { |
||
| 112 | 1 | throw new Exception("Look status and check status in firebase.google.com"); |
|
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 |