Created factory method to build VisitParams from a raw dataset

This commit is contained in:
Alejandro Celaya 2018-11-28 19:58:45 +01:00
parent 45254606d4
commit b0f250ed8a
2 changed files with 15 additions and 11 deletions

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Model; namespace Shlinkio\Shlink\Core\Model;
use Cake\Chronos\Chronos;
use Shlinkio\Shlink\Common\Util\DateRange; use Shlinkio\Shlink\Common\Util\DateRange;
final class VisitsParams final class VisitsParams
@ -21,6 +22,19 @@ final class VisitsParams
$this->itemsPerPage = $itemsPerPage; $this->itemsPerPage = $itemsPerPage;
} }
public static function fromRawData(array $query): self
{
$startDate = self::getDateQueryParam($query, 'startDate');
$endDate = self::getDateQueryParam($query, 'endDate');
return new self(new DateRange($startDate, $endDate), $query['page'] ?? 1, $query['itemsPerPage'] ?? null);
}
private static function getDateQueryParam(array $query, string $key): ?Chronos
{
return ! isset($query[$key]) || empty($query[$key]) ? null : Chronos::parse($query[$key]);
}
public function getDateRange(): DateRange public function getDateRange(): DateRange
{ {
return $this->dateRange; return $this->dateRange;

View File

@ -3,12 +3,10 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Action\Visit; namespace Shlinkio\Shlink\Rest\Action\Visit;
use Cake\Chronos\Chronos;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Model\VisitsParams; use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
@ -38,11 +36,9 @@ class GetVisitsAction extends AbstractRestAction
public function handle(Request $request): Response public function handle(Request $request): Response
{ {
$shortCode = $request->getAttribute('shortCode'); $shortCode = $request->getAttribute('shortCode');
$startDate = $this->getDateQueryParam($request, 'startDate');
$endDate = $this->getDateQueryParam($request, 'endDate');
try { try {
$visits = $this->visitsTracker->info($shortCode, new VisitsParams(new DateRange($startDate, $endDate))); $visits = $this->visitsTracker->info($shortCode, VisitsParams::fromRawData($request->getQueryParams()));
return new JsonResponse([ return new JsonResponse([
'visits' => [ 'visits' => [
@ -57,10 +53,4 @@ class GetVisitsAction extends AbstractRestAction
], self::STATUS_NOT_FOUND); ], self::STATUS_NOT_FOUND);
} }
} }
private function getDateQueryParam(Request $request, string $key): ?Chronos
{
$query = $request->getQueryParams();
return ! isset($query[$key]) || empty($query[$key]) ? null : Chronos::parse($query[$key]);
}
} }