Completed
Push — master ( 800225...4b09e1 )
by Beñat
02:30
created

AjaxManageMenuTranslationAction::__invoke()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 6
nop 1
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\Symfony\Bundle\HttpAction;
13
14
use LIN3S\CMSKernel\Application\Command\Menu\ManageMenuTranslationCommand;
15
use LIN3S\CMSKernel\Domain\Model\Menu\MenuDoesNotExistException;
16
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
17
use LIN3S\SharedKernel\Application\CommandBus;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
22
/**
23
 * @author Beñat Espiña <[email protected]>
24
 */
25 View Code Duplication
class AjaxManageMenuTranslationAction
0 ignored issues
show
Duplication introduced by
This class 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...
26
{
27
    private $commandBus;
28
29
    public function __construct(CommandBus $commandBus)
30
    {
31
        $this->commandBus = $commandBus;
32
    }
33
34
    public function __invoke(Request $request)
35
    {
36
        if (false === $request->isXmlHttpRequest()) {
37
            throw new NotFoundHttpException();
38
        }
39
40
        try {
41
            $this->commandBus->handle(
42
                new ManageMenuTranslationCommand(
43
                    $request->request->get('menu_id'),
44
                    $request->request->get('name'),
45
                    $request->request->get('locale'),
46
                    $request->request->get('items')
47
                )
48
            );
49
50
            return new JsonResponse(null, 200);
51
        } catch (TranslationDoesNotExistException $exception) {
52
            return new JsonResponse('The given translation does not exist', 404);
53
        } catch (MenuDoesNotExistException $exception) {
54
            return new JsonResponse('The given menu does not exist', 404);
55
        }
56
    }
57
}
58