Completed
Push — master ( 076b0c...47e232 )
by Alejandro
10s
created

ResolveShortUrlAction::handle()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

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