TranslationCRUDController::editAction()   B
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 32
nc 16
nop 2
dl 0
loc 54
rs 8.1635
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sludio\HelperBundle\Lexik\Controller;
4
5
use Sludio\HelperBundle\Lexik\Event\RemoveLocaleCacheEvent;
6
use Sonata\AdminBundle\Controller\CRUDController;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
class TranslationCRUDController extends CRUDController
13
{
14
    use NonActionTrait;
0 ignored issues
show
Bug introduced by
The trait Sludio\HelperBundle\Lexi...ntroller\NonActionTrait requires the property $headers which is not provided by Sludio\HelperBundle\Lexi...anslationCRUDController.
Loading history...
15
16
    /**
17
     * @return RedirectResponse|Response
18
     * @throws NotFoundHttpException
19
     */
20
    public function createTransUnitAction()
21
    {
22
        $request = $this->getRequest();
23
        $parameters = $this->getRequest()->request;
24
        if (!$request->isMethod('POST')) {
25
            return $this->renderJson([
26
                'message' => 'method not allowed',
27
            ], 403);
28
        }
29
        $admin = $this->admin;
30
        if (false === $admin->isGranted('EDIT')) {
31
            return $this->renderJson([
32
                'message' => 'access denied',
33
            ], 403);
34
        }
35
        $keyName = $parameters->get('key');
36
        $domainName = $parameters->get('domain');
37
        if (!$keyName || !$domainName) {
38
            return $this->renderJson([
39
                'message' => 'missing key or domain',
40
            ], 422);
41
        }
42
43
        /* @var $transUnitManager \Lexik\Bundle\TranslationBundle\Manager\TransUnitManager */
44
        $transUnitManager = $this->get('lexik_translation.trans_unit.manager');
45
        $transUnit = $transUnitManager->create($keyName, $domainName, true);
46
47
        return $this->editAction($transUnit->getId());
48
    }
49
50
    /**
51
     * Edit action
52
     *
53
     * @param int|string|null $id
54
     *
55
     * @param Request|null    $request
56
     *
57
     * @return RedirectResponse|Response
58
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
59
     */
60
    public function editAction($id = null, Request $request = null)
61
    {
62
        if (!$request) {
63
            $request = $this->getRequest();
64
        }
65
        if (!$request->isMethod('POST')) {
66
            return $this->redirect($this->admin->generateUrl('list'));
67
        }
68
69
        /* @var $transUnit \Lexik\Bundle\TranslationBundle\Model\TransUnit */
70
        $transUnit = $this->get('lexik_translation.translation_storage')->getTransUnitById($id);
71
        if (!$transUnit) {
0 ignored issues
show
introduced by
$transUnit is of type Lexik\Bundle\TranslationBundle\Model\TransUnit, thus it always evaluated to true. If $transUnit can have other possible types, add them to Lexik/Controller/TranslationCRUDController.php:69
Loading history...
72
            throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
73
        }
74
75
        if (false === $this->admin->isGranted('EDIT', $transUnit)) {
76
            return $this->renderJson([
77
                'message' => 'access denied',
78
            ], 403);
79
        }
80
81
        $this->admin->setSubject($transUnit);
82
83
        /* @var $transUnitManager \Lexik\Bundle\TranslationBundle\Manager\TransUnitManager */
84
        $transUnitManager = $this->get('lexik_translation.trans_unit.manager');
85
86
        $parameters = $this->getRequest()->request;
87
88
        $locale = $parameters->get('locale');
89
        $content = $parameters->get('value');
90
91
        if (!$locale) {
92
            return $this->renderJson([
93
                'message' => 'locale missing',
94
            ], 422);
95
        }
96
97
        /* @var $translation \Lexik\Bundle\TranslationBundle\Entity\Translation */
98
        if ($parameters->get('pk')) {
99
            $translation = $transUnitManager->updateTranslation($transUnit, $locale, $content, true);
100
        } else {
101
            $translation = $transUnitManager->addTranslation($transUnit, $locale, $content, null, true);
102
        }
103
104
        if ($request->query->get('clear_cache')) {
105
            $this->get('translator')->removeLocalesCacheFiles([$locale]);
106
        }
107
108
        return $this->renderJson([
109
            'key' => $transUnit->getKey(),
110
            'domain' => $transUnit->getDomain(),
111
            'pk' => $translation->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Lexik\Bundle\TranslationBundle\Model\Translation. It seems like you code against a sub-type of Lexik\Bundle\TranslationBundle\Model\Translation such as Lexik\Bundle\TranslationBundle\Entity\Translation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            'pk' => $translation->/** @scrutinizer ignore-call */ getId(),
Loading history...
112
            'locale' => $translation->getLocale(),
113
            'value' => $translation->getContent(),
114
        ]);
115
    }
116
117
    /**
118
     * @return RedirectResponse
119
     */
120
    public function clearCacheAction()
121
    {
122
        $this->get('event_dispatcher')
123
            ->dispatch(RemoveLocaleCacheEvent::PRE_REMOVE_LOCAL_CACHE, new RemoveLocaleCacheEvent($this->getManagedLocales()));
124
        $this->get('translator')->removeLocalesCacheFiles($this->getManagedLocales());
125
        $this->get('event_dispatcher')
126
            ->dispatch(RemoveLocaleCacheEvent::POST_REMOVE_LOCAL_CACHE, new RemoveLocaleCacheEvent($this->getManagedLocales()));
127
128
        /** @var $session Session */
129
        $session = $this->get('session');
130
        $session->getFlashBag()->set('sonata_flash_success', 'translations.cache_removed');
131
132
        return $this->redirect($this->admin->generateUrl('list'));
133
    }
134
}
135