Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:20
Show Gist options
  • Save fesor/4ee17427abc2b291f9a3 to your computer and use it in GitHub Desktop.
Save fesor/4ee17427abc2b291f9a3 to your computer and use it in GitHub Desktop.
Example
<?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'
]);
);
}
// ...
}
<?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;
}
}
<?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;
}
}
<?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