Created
January 28, 2018 20:02
-
-
Save adrianpietka/500f41ce866382bc1c382f7a0022415c to your computer and use it in GitHub Desktop.
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 CreateUserUseCase { | |
| public function __construct(Repository $repository) { | |
| } | |
| public function execute(string $username, string $password) { | |
| } | |
| } | |
| // | |
| // READ | |
| // | |
| class Pagination { | |
| private $limit; | |
| private $offset; | |
| public function __construct(int $limit, int $offset) | |
| { | |
| $this->limitGuard($limit); | |
| $this->offsetGuard($offset, $limit); | |
| $this->limit = $limit; | |
| $this->offset = $offset; | |
| } | |
| public function limit() | |
| { | |
| return $this->limit; | |
| } | |
| public function offset() | |
| { | |
| return $this->offset; | |
| } | |
| private function limitGuard(int $limit) | |
| { | |
| if (!in_array($limit, [10, 20, 30, 40, 50])) { | |
| throw new InvalidLimitException(); | |
| } | |
| } | |
| private function offsetGuard(int $offset, int $limit) | |
| { | |
| if ($offset % $limit !== 0) { | |
| throw new InvalidOffsetException(); | |
| } | |
| } | |
| } | |
| class ResultsList implements JsonSerializable | |
| { | |
| public $results; | |
| public $limit; | |
| public $total; | |
| public $pagination = [ | |
| 'currentPage' => 5, | |
| 'lastPage' => 66 | |
| ]; | |
| public function __construct(array $results, int $limit, int $total, int $offset) | |
| { | |
| $this->results = $results; | |
| $this->limit = $limit; | |
| $this->total = $total; | |
| $this->pagination['currentPage'] = ceil($total / $offset); | |
| $this->pagination['lastPage'] = ceil($total / $limit); | |
| } | |
| public function jsonSerialize() | |
| { | |
| return [ | |
| 'results' => $this->results, | |
| 'limit' => $this->limit, | |
| 'total' => $this->total, | |
| 'pagination' => $this->pagination | |
| ]; | |
| } | |
| } | |
| class UserCriteria | |
| { | |
| private $category; | |
| public function __construct(int $categoryId) | |
| { | |
| $this->category = $categoryId; | |
| } | |
| public function getCategory() | |
| { | |
| return $this->category; | |
| } | |
| } | |
| class UserListingUseCase | |
| { | |
| public function __construct(DBAL $dbal) | |
| { | |
| } | |
| public function execute(UserCriteria $criteria, Pagination $pagination) : ResultsList | |
| { | |
| $query = $dbal->newQueryBuilder(); | |
| if ($criteria->getCategory()) { | |
| $query->filter('category', $criteria->getCategory()); | |
| } | |
| // set limit | |
| // set offset | |
| $results = array_map(function($item) { | |
| return new UserListDto($item); | |
| }, $query->execute()); | |
| return new ResultsList($results, $total, $pagination->limit(), $pagination->offset()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment