Some example which needs refactoring
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 | |
| /** | |
| * Table post | |
| * | |
| * id, | |
| * title, | |
| * body | |
| * author_id | |
| */ | |
| class PostGateway | |
| { | |
| public function getPosts() | |
| { | |
| return $this->db->query("SELECT * FROM posts"); | |
| } | |
| } | |
| class AuthorGateway | |
| { | |
| public function getAuthors($post_ids) | |
| { | |
| // assuming the query is correct | |
| return $this->db->query("SELECT * FROM authors WHERE id IN (:post_ids)", $post_ids); | |
| } | |
| } | |
| class AuthorFactory | |
| { | |
| public function newInstance($data) | |
| { | |
| $author = new Author($data); | |
| return $author; | |
| } | |
| } | |
| class PostFactory | |
| { | |
| public function newInstance($data, Author $author) | |
| { | |
| $post = new Post($data); | |
| $post->author = $author; | |
| return $post; | |
| } | |
| } | |
| class PostService | |
| { | |
| protected $post_gateway; | |
| protected $author_gateway; | |
| public function __construct( | |
| PostGateway $post_gateway, | |
| AuthorGateway $author_gateway, | |
| PostFactory $post_factory, | |
| AuthorFactory $author_factory | |
| ) | |
| { | |
| $this->post_gateway = $post_gateway; | |
| $this->post_factory = $post_factory; | |
| $this->author_gateway = $author_gateway; | |
| $this->author_factory = $author_factory; | |
| } | |
| public function getPosts() | |
| { | |
| $posts = array(); | |
| $posts_result = $this->post_gateway->getPosts(); | |
| $post_ids = array(); | |
| // iterate and get post_ids; | |
| foreach ($posts as $post) { | |
| $post_ids[] = $post['id']; | |
| } | |
| $authors_result = $this->author_gateway->getAuthors(); | |
| // let us iterate and make a key of author_id | |
| foreach ($posts_result as $post) { | |
| $author = $this->author_factory->newInstance($authors_result[$post['author_id']]); | |
| $posts[] = $this->post_factory->newInstance($post, $author); | |
| } | |
| return $posts; | |
| } | |
| } | |
| class Post | |
| { | |
| public $id; | |
| public $title; | |
| public $body; | |
| public $author | |
| } | |
| class Author | |
| { | |
| public $id; | |
| public $username; | |
| public $displayname; | |
| } | |
| class Something | |
| { | |
| public function indexAction() | |
| { | |
| $post_service = new PostService( | |
| new PostGateway(), | |
| new AuthorGateway(), | |
| new PostFactory(), | |
| new AuthorFactory(), | |
| ); | |
| $this->view->posts = $post_service->getPosts(); | |
| } | |
| } |
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 | |
| /** | |
| * Table post | |
| * | |
| * id, | |
| * title, | |
| * body | |
| * author_id | |
| */ | |
| class SomeModel | |
| { | |
| public function getPosts() | |
| { | |
| return $this->db->query("SELECT * FROM POSTS"); | |
| } | |
| } | |
| class Something | |
| { | |
| public function indexAction() | |
| { | |
| $somemodel = new SomeModel( | |
| // pass deps | |
| ); | |
| $posts = $somemodel->getPosts(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment