Completed
Push — master ( f60c21...243075 )
by Alejandro
10:43
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 Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Log\LoggerInterface;
9
use Shlinkio\Shlink\Core\Exception;
10
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
11
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
12
use Shlinkio\Shlink\Rest\Util\RestUtils;
13
use Zend\Diactoros\Response\EmptyResponse;
14
use Zend\Diactoros\Response\JsonResponse;
15
use Zend\I18n\Translator\TranslatorInterface;
16
17
class EditShortCodeAction extends AbstractRestAction
18
{
19
    /**
20
     * @var ShortUrlServiceInterface
21
     */
22
    private $shortUrlService;
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28 3 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
        ShortUrlServiceInterface $shortUrlService,
30
        TranslatorInterface $translator,
31
        LoggerInterface $logger = null
32
    ) {
33 3
        parent::__construct($logger);
34 3
        $this->shortUrlService = $shortUrlService;
35 3
        $this->translator = $translator;
36 3
    }
37
38
    /**
39
     * Process an incoming server request and return a response, optionally delegating
40
     * to the next middleware component to create the response.
41
     *
42
     * @param ServerRequestInterface $request
43
     *
44
     * @return ResponseInterface
45
     * @throws \InvalidArgumentException
46
     */
47 3
    public function handle(ServerRequestInterface $request): ResponseInterface
48
    {
49 3
        $postData = (array) $request->getParsedBody();
50 3
        $shortCode = $request->getAttribute('shortCode', '');
51
52
        try {
53 3
            $this->shortUrlService->updateMetadataByShortCode(
54 3
                $shortCode,
55 3
                ShortUrlMeta::createFromRawData($postData)
56
            );
57 1
            return new EmptyResponse();
58 2
        } catch (Exception\InvalidShortCodeException $e) {
59 1
            $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
60 1
            return new JsonResponse([
61 1
                'error' => RestUtils::getRestErrorCodeFromException($e),
62 1
                'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
63 1
            ], self::STATUS_NOT_FOUND);
64 1
        } catch (Exception\ValidationException $e) {
65 1
            $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
66 1
            return new JsonResponse([
67 1
                'error' => RestUtils::getRestErrorCodeFromException($e),
68 1
                'message' => $this->translator->translate('Provided data is invalid.'),
69 1
            ], self::STATUS_BAD_REQUEST);
70
        }
71
    }
72
}
73