Completed
Pull Request — master (#148)
by Alejandro
04:08
created

ResolveUrlAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 62
ccs 30
cts 30
cp 1
rs 10
wmc 5
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B handle() 0 32 4
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 4
    public function __construct(
32
        UrlShortenerInterface $urlShortener,
33
        TranslatorInterface $translator,
34
        LoggerInterface $logger = null
35
    ) {
36 4
        parent::__construct($logger);
37 4
        $this->urlShortener = $urlShortener;
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
50
        try {
51 4
            $longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
52 1
            return new JsonResponse([
53 1
                'longUrl' => $longUrl,
54
            ]);
55 3
        } catch (InvalidShortCodeException $e) {
56 1
            $this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $e);
57 1
            return new JsonResponse([
58 1
                'error' => RestUtils::getRestErrorCodeFromException($e),
59 1
                'message' => sprintf(
60 1
                    $this->translator->translate('Provided short code "%s" has an invalid format'),
61 1
                    $shortCode
62
                ),
63 1
            ], self::STATUS_BAD_REQUEST);
64 2
        } catch (EntityDoesNotExistException $e) {
65 1
            $this->logger->warning('Provided short code couldn\'t be found.' . PHP_EOL . $e);
66 1
            return new JsonResponse([
67 1
                'error' => RestUtils::INVALID_ARGUMENT_ERROR,
68 1
                'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
69 1
            ], self::STATUS_NOT_FOUND);
70 1
        } catch (\Exception $e) {
71 1
            $this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e);
72 1
            return new JsonResponse([
73 1
                'error' => RestUtils::UNKNOWN_ERROR,
74 1
                'message' => $this->translator->translate('Unexpected error occurred'),
75 1
            ], self::STATUS_INTERNAL_SERVER_ERROR);
76
        }
77
    }
78
}
79