Last active
August 29, 2015 14:20
-
-
Save fesor/4ee17427abc2b291f9a3 to your computer and use it in GitHub Desktop.
Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class KernelEventsSubscriber { | |
public function flushChanges() | |
{ | |
$this->em->flush(); | |
} | |
public function prepareResponse(GetResponseForControllerResultEvent $event) | |
{ | |
$this->flushChanges(); | |
return $event->setResponse( | |
// status code and headers should be provided from annotations or configs... | |
new Response($this->serializer->serialize($event->getResponse()), 200, [ | |
'Content-type': 'application/json' | |
]); | |
); | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RegistrationService | |
{ | |
private $validator; | |
private $repository; | |
public function __construct(UserRepository $repository, Validator $validator) | |
{ | |
$this->validator = $validator; | |
$this->repository = $repository; | |
} | |
public function registerUser($email, $password) | |
{ | |
$user = new User($email, $password); | |
if (!$this->validator->isValid($user)) { | |
throw new \DomainException(); | |
} | |
$this->repository->add($user); | |
return $user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserController extends Controller { | |
public function registerAction(Request $request) | |
{ | |
$user = $this | |
->get('app.user.registrator') | |
->registerUser( | |
$request->get('email'), | |
$request->get('password') | |
); | |
return $user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserRepository | |
{ | |
public function __construct(EntityManagerInterface $em) | |
{ | |
$this->em = $em; | |
} | |
public function add(User $user) | |
{ | |
$this->em->persist($user); | |
} | |
public function remove(User $user) | |
{ | |
$this->em->remove($user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment