My original code is below
/*
Create the Role
*/
$result = (new RoleDb())->Create($obj);
if($result["Success"]) {
/*
| Get all Modules
*/
$Permissions = $this->Module->All($obj->RoleID);
$list = [];
/*
| Prepare the list that will be assigned to Newly created role.
*/
foreach($Permissions["Data"] as $Permission) {
$RolePermissionOM = new RolePermissionOM();
$RolePermissionOM->PermissionID = $Permission->PermissionID;
$RolePermissionOM->IsActive = $Permission->DefaultPermission;
$RolePermissionOM->RoleID = $result["Data"];
array_push($list, $RolePermissionOM);
}
/*
| Create default permissions for above created role.
*/
return $this->RolePermission->CreateDefaultPermissions($list, $result["Data"]);
}
Now, in my application, there are 3 more points where role is being created and instead of code duplication, I though to convert this code into event. SO whenever a role is being created, an Event is being fired to create the permission records for that role.I wrote the below code.
Event::fire(new RoleCreationEvent($result));
// `$result` contains the newly created RoleID.
Question : In my original code, I was able to get the result to check if the permissions are saved correctly or not. How will I do that in case of firing the Event ?
Event Code
class RolePermissionEvent extends Event
{
use SerializesModels;
public function __construct($RoleID, $Module, $RolePermission)
{
$Permissions = $Module->All($RoleID);
$list = [];
foreach($Permissions["Data"] as $Permission) {
$RolePermissionOM = new RolePermissionOM();
$RolePermissionOM->PermissionID = $Permission->PermissionID;
$RolePermissionOM->RoleID = $RoleID;
array_push($list, $RolePermissionOM);
}
return $RolePermission->CreateDefaultPermissions($list, $RoleID);
}
}