Moved transformer to constructor in some actions, to avoid creating it over and over

This commit is contained in:
Alejandro Celaya 2021-01-31 13:12:56 +01:00
parent ef12e90ae7
commit 85bc5ce595
3 changed files with 9 additions and 14 deletions

View File

@ -16,22 +16,20 @@ use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
abstract class AbstractCreateShortUrlAction extends AbstractRestAction
{
private UrlShortenerInterface $urlShortener;
private array $domainConfig;
private ShortUrlDataTransformer $transformer;
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
{
$this->urlShortener = $urlShortener;
$this->domainConfig = $domainConfig;
$this->transformer = new ShortUrlDataTransformer($domainConfig);
}
public function handle(Request $request): Response
{
$shortUrlMeta = $this->buildShortUrlData($request);
$shortUrl = $this->urlShortener->shorten($shortUrlMeta);
$transformer = new ShortUrlDataTransformer($this->domainConfig);
return new JsonResponse($transformer->transform($shortUrl));
return new JsonResponse($this->transformer->transform($shortUrl));
}
/**

View File

@ -22,12 +22,12 @@ class ListShortUrlsAction extends AbstractRestAction
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
private ShortUrlServiceInterface $shortUrlService;
private array $domainConfig;
private ShortUrlDataTransformer $transformer;
public function __construct(ShortUrlServiceInterface $shortUrlService, array $domainConfig)
{
$this->shortUrlService = $shortUrlService;
$this->domainConfig = $domainConfig;
$this->transformer = new ShortUrlDataTransformer($domainConfig);
}
public function handle(Request $request): Response
@ -36,8 +36,6 @@ class ListShortUrlsAction extends AbstractRestAction
ShortUrlsParams::fromRawData($request->getQueryParams()),
AuthenticationMiddleware::apiKeyFromRequest($request),
);
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer(
$this->domainConfig,
))]);
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, $this->transformer)]);
}
}

View File

@ -19,22 +19,21 @@ class ResolveShortUrlAction extends AbstractRestAction
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
private ShortUrlResolverInterface $urlResolver;
private array $domainConfig;
private ShortUrlDataTransformer $transformer;
public function __construct(ShortUrlResolverInterface $urlResolver, array $domainConfig)
{
$this->urlResolver = $urlResolver;
$this->domainConfig = $domainConfig;
$this->transformer = new ShortUrlDataTransformer($domainConfig);
}
public function handle(Request $request): Response
{
$transformer = new ShortUrlDataTransformer($this->domainConfig);
$url = $this->urlResolver->resolveShortUrl(
ShortUrlIdentifier::fromApiRequest($request),
AuthenticationMiddleware::apiKeyFromRequest($request),
);
return new JsonResponse($transformer->transform($url));
return new JsonResponse($this->transformer->transform($url));
}
}