Skip to content

Instantly share code, notes, and snippets.

@reshadman
Created April 24, 2016 06:33
Show Gist options
  • Save reshadman/7d348632f666693e1875f557151dfddb to your computer and use it in GitHub Desktop.
Save reshadman/7d348632f666693e1875f557151dfddb to your computer and use it in GitHub Desktop.
<?php
class UserController {
public function postCreate(UserCreatorService $userCreatorService, Request $request)
{
$creationNeeds = new CreationValueObject;
$creationNeeds->username = $request->username;
$creationNeeds->password = $request->password;
$creationNeeds->email = $request->email;
$creationNeeds->name = $request->name;
$user = $userCreatorService->create($creationNeeds);
return redirect()->back()->with('success_message', 'User successfully created');
}
}
class ApiUserController {
public function postCreate(UserCreatorService $userCreatorService, Request $request)
{
$creationNeeds = new CreationValueObject;
$creationNeeds->username = $request->username;
$creationNeeds->password = $request->password;
$creationNeeds->email = $request->email;
$creationNeeds->name = $request->name;
$user = $userCreatorService->create($creationNeeds);
return new JsonResponse(['user' => $user->toArray()]);
}
}
class CreationValueObject {
public $username;
public $email;
public $password;
public $name;
}
class UserCreatorService {
protected $userRepo;
protected $notifRepo;
public function __construct(UserRepostory $userRepo, NotificationRepository $notifRepo)
{
$this->userRepo = $userRepo;
$this->notifRepo = $notifRepo;
}
public function createUser(CreationValueObject $createNeeds)
{
$user = $this->userRepo->create(get_object_vars($createNeeds));
$this->notifRepo->creationNotifcation('Some somy data');
event(new UserCreatedEvent($user));
return $user;
}
}
class UserRepository {
public function create(array $data)
{
return User::create($data);
}
}
class UserCreatedEvent {
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment