mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
<?php
|
|
namespace Shlinkio\Shlink\Rest\Action;
|
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Log\LoggerInterface;
|
|
use Psr\Log\NullLogger;
|
|
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService;
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
|
use Zend\Diactoros\Response\JsonResponse;
|
|
use Zend\I18n\Translator\TranslatorInterface;
|
|
|
|
class ListShortcodesAction extends AbstractRestAction
|
|
{
|
|
use PaginatorUtilsTrait;
|
|
|
|
/**
|
|
* @var ShortUrlServiceInterface
|
|
*/
|
|
private $shortUrlService;
|
|
/**
|
|
* @var TranslatorInterface
|
|
*/
|
|
private $translator;
|
|
|
|
/**
|
|
* ListShortcodesAction constructor.
|
|
* @param ShortUrlServiceInterface|ShortUrlService $shortUrlService
|
|
* @param TranslatorInterface $translator
|
|
* @param LoggerInterface $logger
|
|
*
|
|
* @Inject({ShortUrlService::class, "translator", "Logger_Shlink"})
|
|
*/
|
|
public function __construct(
|
|
ShortUrlServiceInterface $shortUrlService,
|
|
TranslatorInterface $translator,
|
|
LoggerInterface $logger = null
|
|
) {
|
|
parent::__construct($logger);
|
|
$this->shortUrlService = $shortUrlService;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
/**
|
|
* @param Request $request
|
|
* @param Response $response
|
|
* @param callable|null $out
|
|
* @return null|Response
|
|
*/
|
|
public function dispatch(Request $request, Response $response, callable $out = null)
|
|
{
|
|
try {
|
|
$query = $request->getQueryParams();
|
|
$shortUrls = $this->shortUrlService->listShortUrls(isset($query['page']) ? $query['page'] : 1);
|
|
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]);
|
|
} catch (\Exception $e) {
|
|
$this->logger->error('Unexpected error while listing short URLs.' . PHP_EOL . $e);
|
|
return new JsonResponse([
|
|
'error' => RestUtils::UNKNOWN_ERROR,
|
|
'message' => $this->translator->translate('Unexpected error occurred'),
|
|
], 500);
|
|
}
|
|
}
|
|
}
|