Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface UserRepository
- {
- function add(User $user);
- }
- ###############
- class DoctrineUserRepository implements UserRepository
- {
- private $doctrine;
- public function __construct(Registry $doctrine)
- {
- $this->doctrine = $doctrine;
- }
- function add(User $user)
- {
- $this->doctrine->getEntityManager()->getConnection()->beginTransaction();
- try{
- $this->doctrine->getEntityManager()->persist($user);
- $this->doctrine->getEntityManager()->flush();
- $this->doctrine->getEntityManager()->commit();
- } catch(\Exception $e){
- $this->doctrine->getEntityManager()->rollback();
- $this->doctrine->getEntityManager()->close();
- throw $e;
- }
- }
- }
- ###############
- class User
- {
- private $username;
- private $password;
- public function __construct($username)
- {
- $this->username = $username;
- }
- public static function create(array $data)
- {
- $user = new User;
- $user->username = $data['username'];
- $user->password = $data['password'];
- return $user;
- }
- }
- #################
- interface UserFactory
- {
- public function createUser(array $data);
- }
- class SecuredPasswordUserFactory implements UserFactory
- {
- public function createUser(array $data)
- {
- $user = new User($data['username']);
- $user->setPassword(md5($data['password']));
- return $user;
- }
- }
- ################
- class RegistrationService
- {
- private $users;
- private $userFactory;
- public function __construct(UserRepository $users, UserFactory $factory)
- {
- $this->users = $users;
- $this->userFactory = $factory;
- }
- public function register(array $data)
- {
- $user = $this->userFactory->createUser($data);
- try {
- $this->users->add($user);
- } catch (\Exception $e) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement