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. | |||||
* | |||||
* @param array $deviceIds A list of device id's to send the notification to | |||||
* @param array $data The data to include in the notification. | |||||
machniak: Documentation for params missing. | |||||
* | |||||
* @throws \Exception on notification failure | |||||
* @return bool true if a notification has been sent | |||||
Done Inline ActionsI'd add @throws info. machniak: I'd add @throws info. | |||||
*/ | |||||
private static function pushFirebaseNotification($deviceIds, $data): bool | |||||
{ | |||||
\Log::debug("sending notification to " . var_export($deviceIds, true)); | |||||
$apiKey = \config('firebase.api_key'); | |||||
$client = new \GuzzleHttp\Client( | |||||
[ | |||||
'verify' => \config('firebase.api_verify_tls') | |||||
] | |||||
Done Inline ActionsShort array syntax, please. machniak: Short array syntax, please. | |||||
); | |||||
$response = $client->request( | |||||
'POST', | |||||
\config('firebase.api_url'), | |||||
[ | |||||
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. | |||||
'headers' => [ | |||||
'Authorization' => "key={$apiKey}", | |||||
], | |||||
'json' => [ | |||||
'registration_ids' => $deviceIds, | |||||
'data' => $data | |||||
] | |||||
] | |||||
); | |||||
if ($response->getStatusCode() != 200) { | |||||
throw new \Exception('FCM Send Error: ' . $response->getStatusCode()); | |||||
} | |||||
return true; | |||||
} | |||||
/** | |||||
* Send a notification to a user. | |||||
* | |||||
* @throws \Exception on notification failure | |||||
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 | |||||
* @return bool true if a notification has been sent | |||||
*/ | |||||
public static function notifyUser($userId, $data): bool | |||||
{ | |||||
$notificationTokens = \App\CompanionApp::where('user_id', $userId) | |||||
->where('mfa_enabled', true) | |||||
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 | |||||
->pluck('notification_token') | |||||
->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.