Last active
August 29, 2015 14:15
-
-
Save l3l0/5a10dffb0db102a6d94a to your computer and use it in GitHub Desktop.
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 | |
$userRepository = new GitClientRepositoryDecoratedUserRepository(new DoctrineUserRepository($this->manager), new GitClient()); | |
// or | |
// $userRepository = new GitClientRepositoryDecoratedUserRepository(new InMemoryUserRepository, new GitClient()); | |
// or | |
// $userRepository = new DoctrineUserRepository($this->manager); | |
// or | |
// $userRepository = new InMemoryUserRepository() | |
// logic do not change only adapters changes | |
$user = $userRepository->find(new Email('leszek.prabucki@gmail.com')); | |
if ($user->hasGitRepositores()) { | |
//some logic | |
} |
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 | |
final class DoctrineUserRepository implements UserRepository | |
{ | |
private $objectManager; | |
public function __construct(ObjectManager $objectManager) | |
{ | |
$this->objectManager = $objectManager; | |
} | |
public function find(Email $email) | |
{ | |
return $this->objectManager->getRepository(User::class)->findOneBy(['email' => (string) $email]); | |
} | |
public function add(User $user) | |
{ | |
$this->objectManager->persist($user); | |
$this->objectManager->flush($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 | |
final class GitClientRepositoryDecoratedUserRepository implements UserRepository | |
{ | |
private $userRepository; | |
private $gitClient; | |
public function __construct(UserRepository $userRepository, GitClient $gitClient) | |
{ | |
$this->userRepository = $userRepository; | |
$this->gitClient = $gitClient; | |
} | |
public function find(Email $email) | |
{ | |
$user = $this->userRepository->find($email); | |
if ($user) { | |
$this->addExternalRepositoriesFor($user); | |
return $user; | |
} | |
} | |
public function add(User $user) | |
{ | |
$this->addExternalRepositoriesFor($user); | |
$this->userRepository->add($user); | |
} | |
private function addExternalRepositoriesFor(User $user) | |
{ | |
foreach ($this->getGitRepositories($user->getEmail() as $repository) { | |
$user->addGitRepository($repository); | |
} | |
} | |
private function getGitRepositories(Email $email) | |
{ | |
return array_map( | |
function ($gitRepositoryFromExternalApi) { | |
return new GitRepository($gitRepositoryFromExternalApi['name']); | |
}, | |
$this->gitClient->getRepositoriesByEmail((string) $email) | |
); | |
} | |
} |
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 | |
final class InMemoryUserRepository implements UserRepository | |
{ | |
private $users; | |
public function find(Email $email) | |
{ | |
$users = array_filter($this->users, function (User $user) use ($email) { return $user->getEmail() == $email; }); | |
if ($users) { | |
return reset($users); | |
} | |
} | |
public function add(User $user) | |
{ | |
$this->users[] = $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 User | |
{ | |
private $email; | |
private $repositories; | |
public function __construct(Email $email) | |
{ | |
$this->email = (string) $email; | |
$this->repositories = new ArrayCollection(); | |
} | |
public function getEmail() | |
{ | |
return new Email($this->email); | |
} | |
public function addGitRepository(GitRepository $repository) | |
{ | |
if (!$this->repositories->contains($repository)) { | |
$this->repositories->add($repository); | |
} | |
} | |
public function hasGitRepositories() | |
{ | |
return $this->repositores->count(); | |
} | |
} |
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 | |
interface UserRepository | |
{ | |
/** | |
* @return User | |
*/ | |
public function find(Email $email); | |
public function add(User $user); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment