mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-22 15:13:59 -06:00
Moved transformer to constructor in some actions, to avoid creating it over and over
This commit is contained in:
parent
ef12e90ae7
commit
85bc5ce595
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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)]);
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user