Created
November 14, 2011 14:14
-
-
Save lstrojny/1364025 to your computer and use it in GitHub Desktop.
EntityExample
This file contains hidden or 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 User | |
{ | |
} | |
class ActivityGroup | |
{ | |
} | |
interface UserFactory | |
{ | |
public function create($email); | |
} | |
interface ActivityGroupFactory | |
{ | |
public function create($name, $description); | |
} | |
class SymfonyUserFactory implements UserFactory, ContainerAware | |
{ | |
protected $container; | |
public function setContainer(Container $container) | |
{ | |
$this->container = $container; | |
} | |
public function create($email) | |
{ | |
$this->container->enterScope('UserBundle:entities:user'); | |
$this->container->setParameter('UserBundle:entities:user.email', $email); | |
$entity = $this->container->get('UserBundle:entities.user'); | |
$this->container->leaveScope('UserBundle:entities:user'); | |
return $entity; | |
} | |
} | |
class SymfonyActivityGroupFactory implements ActivityGroupFactory | |
{ | |
public function create($name, User $moderator) | |
{ | |
return new ActivityGroup($name, $moderator); | |
} | |
} | |
class ActivityGroupController | |
{ | |
private $userFactory; | |
private $userRepository; | |
private $activityGroupFactory; | |
private $em; | |
public function post() | |
{ | |
$moderator = $this->userRepository->findByEmail(...); | |
if (!$moderator) { | |
$moderator = $this->userFactory->create(...); | |
} | |
$activityGroup = $this->activityGroupFactory->create('My funky activity group', $moderator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment