|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Core\Action; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
9
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Action\Util\ErrorResponseBuilderTrait; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Options\AppOptions; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractTrackingAction implements MiddlewareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use ErrorResponseBuilderTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var UrlShortenerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $urlShortener; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var VisitsTrackerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $visitTracker; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var AppOptions |
|
31
|
|
|
*/ |
|
32
|
|
|
private $appOptions; |
|
33
|
|
|
|
|
34
|
4 |
|
public function __construct( |
|
35
|
|
|
UrlShortenerInterface $urlShortener, |
|
36
|
|
|
VisitsTrackerInterface $visitTracker, |
|
37
|
|
|
AppOptions $appOptions |
|
38
|
|
|
) { |
|
39
|
4 |
|
$this->urlShortener = $urlShortener; |
|
40
|
4 |
|
$this->visitTracker = $visitTracker; |
|
41
|
4 |
|
$this->appOptions = $appOptions; |
|
42
|
4 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Process an incoming server request and return a response, optionally delegating |
|
46
|
|
|
* to the next middleware component to create the response. |
|
47
|
|
|
* |
|
48
|
|
|
* @param ServerRequestInterface $request |
|
49
|
|
|
* @param RequestHandlerInterface $handler |
|
50
|
|
|
* |
|
51
|
|
|
* @return ResponseInterface |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
54
|
|
|
{ |
|
55
|
4 |
|
$shortCode = $request->getAttribute('shortCode', ''); |
|
56
|
4 |
|
$query = $request->getQueryParams(); |
|
57
|
4 |
|
$disableTrackParam = $this->appOptions->getDisableTrackParam(); |
|
58
|
|
|
|
|
59
|
|
|
try { |
|
60
|
4 |
|
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode); |
|
61
|
|
|
|
|
62
|
|
|
// Track visit to this short code |
|
63
|
3 |
|
if ($disableTrackParam === null || ! \array_key_exists($disableTrackParam, $query)) { |
|
64
|
2 |
|
$this->visitTracker->track($shortCode, $request); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return $this->createResp($longUrl); |
|
68
|
1 |
|
} catch (InvalidShortCodeException | EntityDoesNotExistException $e) { |
|
69
|
1 |
|
return $this->buildErrorResponse($request, $handler); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
abstract protected function createResp(string $longUrl): ResponseInterface; |
|
74
|
|
|
} |
|
75
|
|
|
|