|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Rest\Action\Visit; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; |
|
10
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; |
|
13
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
14
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
15
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
class GetVisitsAction extends AbstractRestAction |
|
18
|
|
|
{ |
|
19
|
|
|
protected const ROUTE_PATH = '/short-codes/{shortCode}/visits'; |
|
20
|
|
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var VisitsTrackerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $visitsTracker; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TranslatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $translator; |
|
30
|
|
|
|
|
31
|
4 |
|
public function __construct( |
|
32
|
|
|
VisitsTrackerInterface $visitsTracker, |
|
33
|
|
|
TranslatorInterface $translator, |
|
34
|
|
|
LoggerInterface $logger = null |
|
35
|
|
|
) { |
|
36
|
4 |
|
parent::__construct($logger); |
|
37
|
4 |
|
$this->visitsTracker = $visitsTracker; |
|
38
|
4 |
|
$this->translator = $translator; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @return Response |
|
44
|
|
|
* @throws \InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function handle(Request $request): Response |
|
47
|
|
|
{ |
|
48
|
4 |
|
$shortCode = $request->getAttribute('shortCode'); |
|
49
|
4 |
|
$startDate = $this->getDateQueryParam($request, 'startDate'); |
|
50
|
4 |
|
$endDate = $this->getDateQueryParam($request, 'endDate'); |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
4 |
|
$visits = $this->visitsTracker->info($shortCode, new DateRange($startDate, $endDate)); |
|
54
|
|
|
|
|
55
|
2 |
|
return new JsonResponse([ |
|
56
|
|
|
'visits' => [ |
|
57
|
2 |
|
'data' => $visits, |
|
58
|
|
|
], |
|
59
|
|
|
]); |
|
60
|
2 |
|
} catch (InvalidArgumentException $e) { |
|
61
|
1 |
|
$this->logger->warning('Provided nonexistent shortcode' . PHP_EOL . $e); |
|
62
|
1 |
|
return new JsonResponse([ |
|
63
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
64
|
1 |
|
'message' => sprintf( |
|
65
|
1 |
|
$this->translator->translate('Provided short code %s does not exist'), |
|
66
|
1 |
|
$shortCode |
|
67
|
|
|
), |
|
68
|
1 |
|
], self::STATUS_NOT_FOUND); |
|
69
|
1 |
|
} catch (\Exception $e) { |
|
70
|
1 |
|
$this->logger->error('Unexpected error while parsing short code' . PHP_EOL . $e); |
|
71
|
1 |
|
return new JsonResponse([ |
|
72
|
1 |
|
'error' => RestUtils::UNKNOWN_ERROR, |
|
73
|
1 |
|
'message' => $this->translator->translate('Unexpected error occurred'), |
|
74
|
1 |
|
], self::STATUS_INTERNAL_SERVER_ERROR); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Request $request |
|
80
|
|
|
* @param string $key |
|
81
|
|
|
* @return \DateTime|null |
|
82
|
|
|
*/ |
|
83
|
4 |
|
private function getDateQueryParam(Request $request, string $key) |
|
84
|
|
|
{ |
|
85
|
4 |
|
$query = $request->getQueryParams(); |
|
86
|
4 |
|
if (! isset($query[$key]) || empty($query[$key])) { |
|
87
|
4 |
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return new \DateTime($query[$key]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|