I have made some notification preferences in which there are 4 major preferences
- SMS
- PUSH
- FAX
And then there are some specific templates such as booking-accepted and within booking accepted there are again these 4 notifications.
So what I have to do I need to check 1st priority notification preferences first that if SMS is on then I have to check regarding specific templates that SMS is on in booking-accepted and then I'll send the notification or will just unset the recipient.
The object on notification preferences is as follows
{
"sendSms": "1",
"sendFax": "1",
"sendEmail": "1",
"sendPush": "0",
"specific-templates": [
{
"update-video-physical-address": [
{
"sendPush": "1",
"sendSms": "0",
"sendEmail": "0"
}
],
"booking-denied": [
{
"sendPush": "1",
"sendSms": "0"
}
]
}
],
}
And The code I did for this is given below which is bit messy and unclean.
foreach ($recipients as $recipient) {
$user = $userRepository->findByParams([
'email' => $recipient->getUser()->email
]);
if (!is_null($user->userNotificationPreference) && array_key_exists($sendMethod, $user->userNotificationPreference->preferences)
&& $user->userNotificationPreference->preferences[$sendMethod] == true) {
if (array_key_exists($notification->getTemplateCode(), $user->userNotificationPreference->preferences['specific-templates'][0])) {
if (array_key_exists($notification->getTemplateCode(), $user->userNotificationPreference->preferences['specific-templates'][0])
&& array_key_exists($sendMethod, $user->userNotificationPreference->preferences['specific-templates'][0][$notification->getTemplateCode()][0])) {
if ($user->userNotificationPreference->preferences['specific-templates'][0][$notification->getTemplateCode()][0][$sendMethod] == false) {
$key = array_search($recipient, $recipients->toArray());
unset($recipients[$key]);
}
}
}
} elseif (!is_null($user->userNotificationPreference) && array_key_exists($sendMethod, $user->userNotificationPreference->preferences)
&& $user->userNotificationPreference->preferences[$sendMethod] == false) {
$key = array_search($recipient, $recipients->toArray());
unset($recipients[$key]);
}
}
return $recipients;
- getTemplateCode() will return the template code i.e
booking-accepted - $sendMethod is method i.e
sendSms,sendPush
Please help me with writing this mode more clean and efficient way such as reducing IFs and any other approach