1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\Rest\Action; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
5
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait; |
10
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlService; |
11
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; |
12
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
13
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
14
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
class ListShortcodesAction extends AbstractRestAction |
17
|
|
|
{ |
18
|
|
|
use PaginatorUtilsTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ShortUrlServiceInterface |
22
|
|
|
*/ |
23
|
|
|
private $shortUrlService; |
24
|
|
|
/** |
25
|
|
|
* @var TranslatorInterface |
26
|
|
|
*/ |
27
|
|
|
private $translator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ListShortcodesAction constructor. |
31
|
|
|
* @param ShortUrlServiceInterface|ShortUrlService $shortUrlService |
32
|
|
|
* @param TranslatorInterface $translator |
33
|
|
|
* @param LoggerInterface $logger |
34
|
|
|
* |
35
|
|
|
* @Inject({ShortUrlService::class, "translator", "Logger_Shlink"}) |
36
|
|
|
*/ |
37
|
2 |
View Code Duplication |
public function __construct( |
|
|
|
|
38
|
|
|
ShortUrlServiceInterface $shortUrlService, |
39
|
|
|
TranslatorInterface $translator, |
40
|
|
|
LoggerInterface $logger = null |
41
|
|
|
) { |
42
|
2 |
|
parent::__construct($logger); |
43
|
2 |
|
$this->shortUrlService = $shortUrlService; |
44
|
2 |
|
$this->translator = $translator; |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Request $request |
49
|
|
|
* @param Response $response |
50
|
|
|
* @param callable|null $out |
51
|
|
|
* @return null|Response |
52
|
|
|
*/ |
53
|
2 |
|
public function dispatch(Request $request, Response $response, callable $out = null) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
2 |
|
$params = $this->queryToListParams($request->getQueryParams()); |
57
|
2 |
|
$shortUrls = $this->shortUrlService->listShortUrls(...$params); |
|
|
|
|
58
|
1 |
|
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]); |
|
|
|
|
59
|
1 |
|
} catch (\Exception $e) { |
60
|
1 |
|
$this->logger->error('Unexpected error while listing short URLs.' . PHP_EOL . $e); |
61
|
1 |
|
return new JsonResponse([ |
62
|
1 |
|
'error' => RestUtils::UNKNOWN_ERROR, |
63
|
1 |
|
'message' => $this->translator->translate('Unexpected error occurred'), |
64
|
1 |
|
], 500); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $query |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function queryToListParams(array $query) |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
isset($query['page']) ? $query['page'] : 1, |
76
|
|
|
isset($query['searchTerm']) ? $query['searchTerm'] : null, |
77
|
|
|
isset($query['tags']) ? $query['tags'] : [], |
78
|
|
|
isset($query['orderBy']) ? $query['orderBy'] : null, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.