Completed
Pull Request — master (#98)
by Alejandro
06:29
created

UpdateTagAction::process()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 18

Duplication

Lines 8
Ratio 32 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 4
nop 2
dl 8
loc 25
ccs 17
cts 17
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace Shlinkio\Shlink\Rest\Action\Tag;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Log\LoggerInterface;
9
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
10
use Shlinkio\Shlink\Core\Service\Tag\TagService;
11
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
12
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\Response\EmptyResponse;
15
use Zend\Diactoros\Response\JsonResponse;
16
use Zend\I18n\Translator\Translator;
17
use Zend\I18n\Translator\TranslatorInterface;
18
19
class UpdateTagAction extends AbstractRestAction
20
{
21
    /**
22
     * @var TagServiceInterface
23
     */
24
    private $tagService;
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * UpdateTagAction constructor.
32
     * @param TagServiceInterface $tagService
33
     * @param TranslatorInterface $translator
34
     * @param LoggerInterface|null $logger
35
     *
36
     * @DI\Inject({TagService::class, Translator::class, LoggerInterface::class})
37
     */
38 5
    public function __construct(
39
        TagServiceInterface $tagService,
40
        TranslatorInterface $translator,
41
        LoggerInterface $logger = null
42
    ) {
43 5
        parent::__construct($logger);
44 5
        $this->tagService = $tagService;
45 5
        $this->translator = $translator;
46 5
    }
47
48
    /**
49
     * Process an incoming server request and return a response, optionally delegating
50
     * to the next middleware component to create the response.
51
     *
52
     * @param ServerRequestInterface $request
53
     * @param DelegateInterface $delegate
54
     *
55
     * @return ResponseInterface
56
     * @throws \InvalidArgumentException
57
     */
58 5
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
59
    {
60 5
        $body = $request->getParsedBody();
61 5 View Code Duplication
        if (! isset($body['oldName'], $body['newName'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62 3
            return new JsonResponse([
63 3
                'error' => RestUtils::INVALID_ARGUMENT_ERROR,
64 3
                'message' => $this->translator->translate(
65 3
                    'You have to provide both \'oldName\' and \'newName\' params in order to properly rename the tag'
66
                ),
67 3
            ], self::STATUS_BAD_REQUEST);
68
        }
69
70
        try {
71 2
            $this->tagService->renameTag($body['oldName'], $body['newName']);
72 1
            return new EmptyResponse();
73 1
        } catch (EntityDoesNotExistException $e) {
74 1
            return new JsonResponse([
75 1
                'error' => RestUtils::NOT_FOUND_ERROR,
76 1
                'message' => sprintf(
77 1
                    $this->translator->translate('It wasn\'t possible to find a tag with name \'%s\''),
78 1
                    $body['oldName']
79
                ),
80 1
            ], self::STATUS_NOT_FOUND);
81
        }
82
    }
83
}
84