Completed
Pull Request — master (#148)
by Alejandro
02:40
created

ResolveUrlAction::handle()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 26
nc 7
nop 1
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Action\ShortCode;
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\Core\Exception\EntityDoesNotExistException;
10
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
11
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
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 ResolveUrlAction extends AbstractRestAction
18
{
19
    protected const ROUTE_PATH = '/short-codes/{shortCode}';
20
    protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
21
22
    /**
23
     * @var UrlShortenerInterface
24
     */
25
    private $urlShortener;
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    private $translator;
30
31
    public function __construct(
32
        UrlShortenerInterface $urlShortener,
33
        TranslatorInterface $translator,
34
        LoggerInterface $logger = null
35
    ) {
36
        parent::__construct($logger);
37
        $this->urlShortener = $urlShortener;
38
        $this->translator = $translator;
39
    }
40
41
    /**
42
     * @param Request $request
43
     * @return Response
44
     * @throws \InvalidArgumentException
45
     */
46
    public function handle(Request $request): Response
47
    {
48
        $shortCode = $request->getAttribute('shortCode');
49
50
        try {
51
            $longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
52
            return new JsonResponse([
53
                'longUrl' => $longUrl,
54
            ]);
55
        } catch (InvalidShortCodeException $e) {
56
            $this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $e);
57
            return new JsonResponse([
58
                'error' => RestUtils::getRestErrorCodeFromException($e),
59
                'message' => sprintf(
60
                    $this->translator->translate('Provided short code "%s" has an invalid format'),
61
                    $shortCode
62
                ),
63
            ], self::STATUS_BAD_REQUEST);
64
        } catch (EntityDoesNotExistException $e) {
65
            $this->logger->warning('Provided short code couldn\'t be found.' . PHP_EOL . $e);
66
            return new JsonResponse([
67
                'error' => RestUtils::INVALID_ARGUMENT_ERROR,
68
                'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
69
            ], self::STATUS_NOT_FOUND);
70
        } catch (\Exception $e) {
71
            $this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e);
72
            return new JsonResponse([
73
                'error' => RestUtils::UNKNOWN_ERROR,
74
                'message' => $this->translator->translate('Unexpected error occurred'),
75
            ], self::STATUS_INTERNAL_SERVER_ERROR);
76
        }
77
    }
78
}
79