Changeset View
Changeset View
Standalone View
Standalone View
src/app/CompanionApp.php
- This file was added.
<?php | |||||
namespace App; | |||||
use Illuminate\Database\Eloquent\Model; | |||||
/** | |||||
* The eloquent definition of a CompanionApp. | |||||
* | |||||
* A CompanionApp is an kolab companion app that the user registered | |||||
*/ | |||||
class CompanionApp extends Model | |||||
{ | |||||
protected $fillable = [ | |||||
'name', | |||||
'user_id', | |||||
'device_id', | |||||
'notification_token', | |||||
'mfa_enabled', | |||||
]; | |||||
/** | |||||
* Send a notification via firebase. | |||||
* | |||||
* @return bool true if a notification has been sent | |||||
machniak: Documentation for params missing. | |||||
*/ | |||||
private static function pushFirebaseNotification($deviceIds, $data) | |||||
{ | |||||
\Log::debug("sending notification to " . var_export($deviceIds, true)); | |||||
Done Inline ActionsI'd add @throws info. machniak: I'd add @throws info. | |||||
$url = \config('firebase.api_url'); | |||||
$apiKey = \config('firebase.api_key'); | |||||
$fields = [ | |||||
'registration_ids' => $deviceIds, | |||||
'data' => $data | |||||
]; | |||||
$headers = array( | |||||
machniakUnsubmitted Done Inline ActionsShort array syntax, please. machniak: Short array syntax, please. | |||||
'Content-Type:application/json', | |||||
"Authorization:key={$apiKey}" | |||||
); | |||||
$ch = curl_init(); | |||||
machniakUnsubmitted Done Inline ActionsIt would be better to not use curl, but guzzlehttp/guzzle client we already have in use. machniak: It would be better to not use curl, but guzzlehttp/guzzle client we already have in use. | |||||
curl_setopt($ch, CURLOPT_URL, $url); | |||||
curl_setopt($ch, CURLOPT_POST, true); | |||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | |||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); | |||||
$result = curl_exec($ch); | |||||
if ($result === false) { | |||||
throw new \Exception('FCM Send Error: ' . curl_error($ch)); | |||||
} | |||||
curl_close($ch); | |||||
return $result; | |||||
} | |||||
/** | |||||
* Send a notification to a user. | |||||
* | |||||
* @return bool true if a notification has been sent | |||||
*/ | |||||
public static function notifyUser($userId, $data) | |||||
machniakUnsubmitted Done Inline ActionsJust a not that in most cases we're using return type declarations. machniak: Just a not that in most cases we're using return type declarations. | |||||
{ | |||||
Done Inline ActionsAdd @throws machniak: Add @throws | |||||
$notificationTokens = \App\CompanionApp::where('user_id', $userId) | |||||
->where('mfa_enabled', true) | |||||
->get() | |||||
->map(function ($app) { | |||||
return $app->notification_token; | |||||
machniakUnsubmitted Done Inline ActionsUse pluck() method https://laravel.com/docs/8.x/queries#retrieving-a-list-of-column-values machniak: Use pluck() method https://laravel.com/docs/8.x/queries#retrieving-a-list-of-column-values | |||||
}) | |||||
->all(); | |||||
if (empty($notificationTokens)) { | |||||
\Log::debug("There is no 2fa device to notify."); | |||||
return false; | |||||
} | |||||
self::pushFirebaseNotification($notificationTokens, $data); | |||||
return true; | |||||
} | |||||
} |
Documentation for params missing.