Last active
April 9, 2022 21:38
-
-
Save MacDada/e935a24da30db459e2b55711678631ac to your computer and use it in GitHub Desktop.
PHP: Use Case architecture example
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 ShowBlogPostsController | |
{ | |
/** | |
* @param ShowBlogPostUseCase | |
**/ | |
private $useCase; | |
/** | |
* @param TemplateEngine | |
**/ | |
private $templateEngine; | |
public function __construct(ShowBlogPostUseCase $useCase, TemplateEngine $templateEngine) | |
{ | |
$this->useCase = $useCase; | |
$this->templateEngine = $templateEngine; | |
} | |
public function showBlogPostsAction(HttpRequest $httpRequest) | |
{ | |
$useCaseRequest = new ShowBlogPostsRequest(); | |
$useCaseRequest->page = $httpRequest->query->get('page'); | |
$useCaseRequest->categoryName = $httpRequest->query->get('category'); | |
$useCaseResponse = $this->useCase->handle($useCaseRequest); | |
$content = $this->templateEngine->render('blog/posts', [ | |
'posts' => $useCaseResponse->posts, | |
'all_posts_count' => $useCaseResponse->allPostsCount, | |
'categories' => $useCaseResponse->categories, | |
]); | |
return new HttpResponse($content); | |
} | |
} |
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 ShowBlogPostsRequest | |
{ | |
/** | |
* @param int | |
*/ | |
public $page; | |
/** | |
* @param string | |
**/ | |
public $categoryName; | |
} |
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 | |
// simple DTO, but one could create getters instead of public properties ofc | |
class ShowBlogPostsResponse | |
{ | |
/** | |
* @param Post[] | |
**/ | |
public $posts; | |
/** | |
* @param int | |
**/ | |
public $allPostsCount; | |
/** | |
* @param PostCategory[] | |
**/ | |
public $categories; | |
} |
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 ShowBlogPostsUseCase | |
{ | |
/** | |
* @param PostRepository | |
**/ | |
private $postRepository; | |
/** | |
* @param CategoryRepository | |
**/ | |
private $categoryRepository; | |
/** | |
* @param int | |
**/ | |
private $postsLimit; | |
public function __construct( | |
PostRepository $postRepository, | |
CategoryRepository $categoryRepository, | |
$postsLimit | |
) { | |
$this->postRepository = $postRepository; | |
$this->categoryRepository = $categoryRepository; | |
$this->postsLimit = $postsLimit; | |
} | |
public function handle(ShowBlogPostsRequest $request) | |
{ | |
$category = $this->categoryRepository->findOneByName($request->categoryName); | |
$posts = $this->postRepository->findByCategoryAndPage( | |
$category, | |
$request->page, | |
$this->limit | |
); | |
$response = new ShowBlogPostsResponse(); | |
$response->posts = $posts; | |
$response->allPostsCount = $this->postRepository->count(); | |
$response->categories = $this->categoriesRepository->findAll(); | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment