Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

EditShortCodeAction::process()   B

Complexity

Conditions 3
Paths 5

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 20
nc 5
nop 2
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
ccs 19
cts 19
cp 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Action;
5
6
use Interop\Http\ServerMiddleware\DelegateInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Log\LoggerInterface;
10
use Shlinkio\Shlink\Core\Exception;
11
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
12
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\Response\EmptyResponse;
15
use Zend\Diactoros\Response\JsonResponse;
16
use Zend\I18n\Translator\TranslatorInterface;
17
18
class EditShortCodeAction extends AbstractRestAction
19
{
20
    /**
21
     * @var ShortUrlServiceInterface
22
     */
23
    private $shortUrlService;
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
29 3
    public function __construct(
30
        ShortUrlServiceInterface $shortUrlService,
31
        TranslatorInterface $translator,
32
        LoggerInterface $logger = null
33
    ) {
34 3
        parent::__construct($logger);
35 3
        $this->shortUrlService = $shortUrlService;
36 3
        $this->translator = $translator;
37 3
    }
38
39
    /**
40
     * Process an incoming server request and return a response, optionally delegating
41
     * to the next middleware component to create the response.
42
     *
43
     * @param ServerRequestInterface $request
44
     * @param DelegateInterface $delegate
45
     *
46
     * @return ResponseInterface
47
     * @throws \InvalidArgumentException
48
     */
49 3
    public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
50
    {
51 3
        $postData = (array) $request->getParsedBody();
52 3
        $shortCode = $request->getAttribute('shortCode', '');
53
54
        try {
55 3
            $this->shortUrlService->updateMetadataByShortCode(
56 3
                $shortCode,
57 3
                ShortUrlMeta::createFromRawData($postData)
58
            );
59 1
            return new EmptyResponse();
60 2
        } catch (Exception\InvalidShortCodeException $e) {
61 1
            $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
62 1
            return new JsonResponse([
63 1
                'error' => RestUtils::getRestErrorCodeFromException($e),
64 1
                'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
65 1
            ], self::STATUS_NOT_FOUND);
66 1
        } catch (Exception\ValidationException $e) {
67 1
            $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
68 1
            return new JsonResponse([
69 1
                'error' => RestUtils::getRestErrorCodeFromException($e),
70 1
                'message' => $this->translator->translate('Provided data is invalid.'),
71 1
            ], self::STATUS_BAD_REQUEST);
72
        }
73
    }
74
}
75