Skip to content

Commit

Permalink
Refactore more request mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Sep 11, 2023
1 parent db77597 commit cad4fb3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/HttpController/Api/Dto/HistoryRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class HistoryRequestDto
{
private function __construct(
private readonly int $requestedUserId,
private readonly ?string $searchTerm,
private readonly int $page,
private readonly int $limit,
Expand All @@ -16,13 +17,15 @@ private function __construct(
}

public static function create(
int $requestedUserId,
?string $searchTerm,
int $page,
int $limit,
string $sortBy,
SortOrder $sortOrder,
) : self {
return new self(
$requestedUserId,
$searchTerm,
$page,
$limit,
Expand Down Expand Up @@ -55,4 +58,9 @@ public function getSortOrder() : SortOrder
{
return $this->sortOrder;
}

public function getRequestedUserId() : int
{
return $this->requestedUserId;
}
}
8 changes: 8 additions & 0 deletions src/HttpController/Api/Dto/WatchlistRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class WatchlistRequestDto
{
private function __construct(
private readonly int $requestedUserId,
private readonly ?string $searchTerm,
private readonly int $page,
private readonly int $limit,
Expand All @@ -20,6 +21,7 @@ private function __construct(
}

public static function create(
int $requestedUserId,
?string $searchTerm,
int $page,
int $limit,
Expand All @@ -30,6 +32,7 @@ public static function create(
?string $genre = null,
) : self {
return new self(
$requestedUserId,
$searchTerm,
$page,
$limit,
Expand Down Expand Up @@ -80,4 +83,9 @@ public function getSortOrder() : SortOrder
{
return $this->sortOrder;
}

public function getRequestedUserId() : int
{
return $this->requestedUserId;
}
}
9 changes: 2 additions & 7 deletions src/HttpController/Api/HistoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Movary\HttpController\Api;

use Movary\Domain\Movie\History\MovieHistoryApi;
use Movary\Domain\User\UserApi;
use Movary\HttpController\Api\RequestMapper\HistoryRequestMapper;
use Movary\HttpController\Api\ResponseMapper\HistoryResponseMapper;
use Movary\Service\PaginationElementsCalculator;
Expand All @@ -14,7 +13,6 @@
class HistoryController
{
public function __construct(
private readonly UserApi $userApi,
private readonly MovieHistoryApi $movieHistoryApi,
private readonly PaginationElementsCalculator $paginationElementsCalculator,
private readonly HistoryRequestMapper $historyRequestMapper,
Expand All @@ -24,20 +22,17 @@ public function __construct(

public function getHistory(Request $request) : Response
{
$routeParameterUserName = $request->getRouteParameters()['username'] ?? null;
$requestedUser = $this->userApi->fetchUserByName((string)$routeParameterUserName);

$requestData = $this->historyRequestMapper->mapRequest($request);

$historyEntries = $this->movieHistoryApi->fetchHistoryPaginated(
$requestedUser->getId(),
$requestData->getRequestedUserId(),
$requestData->getLimit(),
$requestData->getPage(),
$requestData->getSearchTerm(),
);

$historyCount = $this->movieHistoryApi->fetchHistoryCount(
$requestedUser->getId(),
$requestData->getRequestedUserId(),
$requestData->getSearchTerm(),
);

Expand Down
5 changes: 5 additions & 0 deletions src/HttpController/Api/RequestMapper/HistoryRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class HistoryRequestMapper

private const DEFAULT_SORT_BY = 'watchedAt';

public function __construct(private readonly RequestMapper $requestMapper)
{
}

public function mapRequest(Request $request) : HistoryRequestDto
{
$getParameters = $request->getGetParameters();
Expand All @@ -23,6 +27,7 @@ public function mapRequest(Request $request) : HistoryRequestDto
$limit = $getParameters['limit'] ?? self::DEFAULT_LIMIT;

return HistoryRequestDto::create(
$this->requestMapper->mapUsernameFromRoute($request)->getId(),
$searchTerm,
(int)$page,
(int)$limit,
Expand Down
26 changes: 26 additions & 0 deletions src/HttpController/Api/RequestMapper/RequestMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Movary\HttpController\Api\RequestMapper;

use Movary\Domain\User\UserApi;
use Movary\Domain\User\UserEntity;
use Movary\ValueObject\Http\Request;

class RequestMapper
{
public function __construct(
private readonly UserApi $userApi,
) {
}

public function mapUsernameFromRoute(Request $request) : UserEntity
{
$username = $request->getRouteParameters()['username'] ?? null;

if ($username === null) {
throw new \RuntimeException('Username parameter missing in route');
}

return $this->userApi->fetchUserByName($username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class WatchlistRequestMapper

private const DEFAULT_SORT_BY = 'addedAt';

public function __construct(private readonly RequestMapper $requestMapper)
{
}

public function mapRequest(Request $request) : WatchlistRequestDto
{
$getParameters = $request->getGetParameters();
Expand All @@ -29,6 +33,7 @@ public function mapRequest(Request $request) : WatchlistRequestDto
$releaseYear = $this->mapReleaseYear($getParameters);

return WatchlistRequestDto::create(
$this->requestMapper->mapUsernameFromRoute($request)->getId(),
$searchTerm,
(int)$page,
(int)$limit,
Expand Down
9 changes: 2 additions & 7 deletions src/HttpController/Api/WatchlistController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Movary\HttpController\Api;

use Movary\Domain\Movie\Watchlist\MovieWatchlistApi;
use Movary\Domain\User\Service\Authentication;
use Movary\Domain\User\UserApi;
use Movary\HttpController\Api\RequestMapper\WatchlistRequestMapper;
use Movary\HttpController\Api\ResponseMapper\WatchlistResponseMapper;
Expand All @@ -16,7 +15,6 @@ class WatchlistController
{
public function __construct(
private readonly MovieWatchlistApi $movieWatchlistApi,
private readonly UserApi $userApi,
private readonly PaginationElementsCalculator $paginationElementsCalculator,
private readonly WatchlistRequestMapper $watchlistRequestMapper,
private readonly WatchlistResponseMapper $watchlistResponseMapper,
Expand All @@ -25,13 +23,10 @@ public function __construct(

public function getWatchlist(Request $request) : Response
{
$routeParameterUserName = $request->getRouteParameters()['username'] ?? null;
$requestedUser = $this->userApi->fetchUserByName((string)$routeParameterUserName);

$requestData = $this->watchlistRequestMapper->mapRequest($request);

$watchlistEntries = $this->movieWatchlistApi->fetchWatchlistPaginated(
$requestedUser->getId(),
$requestData->getRequestedUserId(),
$requestData->getLimit(),
$requestData->getPage(),
$requestData->getSearchTerm(),
Expand All @@ -43,7 +38,7 @@ public function getWatchlist(Request $request) : Response
);

$watchlistCount = $this->movieWatchlistApi->fetchWatchlistCount(
$requestedUser->getId(),
$requestData->getRequestedUserId(),
$requestData->getSearchTerm(),
$requestData->getReleaseYear(),
$requestData->getLanguage(),
Expand Down

0 comments on commit cad4fb3

Please sign in to comment.