Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save duwaljyoti/6f9360c5f79e4e8fc64defc15b264a61 to your computer and use it in GitHub Desktop.
Save duwaljyoti/6f9360c5f79e4e8fc64defc15b264a61 to your computer and use it in GitHub Desktop.
Cumque ducimus accu
<?php
public function store(
$data,
User $userModel,
Logger $activityLogger,
Mailer $mailer
)
{
// valdating the user data
if ($data['first_name'] || $data['email']) {
throw new \Exception('Some required fields are missing!');
}
// save the user
$userModel->fill($data);
$userModel->save();
// log the create event of the user.
$loggableId = $userModel->id;
$loggableType = get_class($userModel);
$message = 'User is created.';
if (!$activityLogger->save($loggableId, $loggableType, $message)) {
throw new \Exception('Activity Not saved exception.');
}
// send email to relavant people
if (
!$mailer->send('emails.default', $userModel->email, 'Welcome to our application.')
|| $mailer->send('users.confirmation', 'owner@gm.com', 'A new user has been added.')
) {
throw new \Exception('Something went wrong! Email not sent.');
}
// format the user.
$formattedUserData = [
'full_name' => $userModel->first_name . $userModel->last_name,
'email' => $userModel->email
];
// return necessary respone
return $formattedUserData;
}
public function store(
array $data,
UserService $userService,
ActivityLoggerService $activityLogger,
EmailService $emailService
) {
$validatedData = $this->validate($data);
$userModel = $userService->save($validatedData);
$activityLogger->log($userModel, 'User is created.');
$emailService->send($userModel->email, 'Welcome to our application');
return new UserTransformer($userModel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment