Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. <?php
  2.  
  3. class Category {}
  4.  
  5. interface CategoryRepository {
  6.  
  7. /**
  8. * @return Category[]
  9. */
  10. public function all(): array;
  11.  
  12. public function save(Category $category);
  13.  
  14. }
  15.  
  16. class CategoryService {
  17.  
  18. private $categoryRepository;
  19.  
  20. public function __construct(CategoryRepository $categoryRepository) {
  21. $this->categoryRepository = $categoryRepository;
  22. }
  23.  
  24. public function hardDomainLogic(Category $category) {
  25. $availableCategories = $this->categoryRepository->all();
  26. // ...
  27. }
  28.  
  29. }
  30.  
  31. class TestCategoryService {
  32.  
  33. public function testHardDomainLogic() {
  34. $categoryA = new Category();
  35. $categoryB = new Category();
  36. $categoryC = new Category();
  37.  
  38. $categoryRepository = $this->createCategoryRepository();
  39. $categoryRepository->save($categoryA);
  40. $categoryRepository->save($categoryB);
  41.  
  42. $service = new CategoryService($categoryRepository);
  43. $service->hardDomainLogic($categoryC);
  44. }
  45.  
  46. private function createCategoryRepository(): CategoryRepository {
  47. return new class implements CategoryRepository {
  48.  
  49. private $categories = [];
  50.  
  51. public function save(Category $category) {
  52. $this->categories[] = $category;
  53. }
  54.  
  55. public function all(): array {
  56. return $this->categories;
  57. }
  58. };
  59. }
  60.  
  61. }
  62.  
  63. $test = new TestCategoryService();
  64. $test->testHardDomainLogic();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement