TranslationController::deleteAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
ccs 20
cts 20
cp 1
cc 2
eloc 20
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * This file is part of the AsmTranslationLoaderBundle package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Asm\TranslationLoaderBundle\Controller;
11
12
use Asm\TranslationLoaderBundle\Entity\Translation;
13
use Asm\TranslationLoaderBundle\Model\TranslationManagerInterface;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Templating\EngineInterface;
19
20
/**
21
 * Class TranslationController
22
 *
23
 * @package TranslationLoaderBundle\Controller
24
 * @author Marc Aschmann <[email protected]>
25
 */
26
class TranslationController
27
{
28
    /**
29
     * @var FormFactoryInterface
30
     */
31
    private $formFactory;
32
33
    /**
34
     * @var EngineInterface
35
     */
36
    private $templating;
37
38
    /**
39
     * @var TranslationManagerInterface
40
     */
41
    private $translationManager;
42
43
    /**
44
     * @param EngineInterface $templating
45
     * @param TranslationManagerInterface $translationManager
46
     * @param FormFactoryInterface $formFactory
47
     */
48 5
    public function __construct(
49
        EngineInterface $templating,
50
        TranslationManagerInterface $translationManager,
51
        FormFactoryInterface $formFactory
52
    ) {
53 5
        $this->formFactory = $formFactory;
54 5
        $this->templating = $templating;
55 5
        $this->translationManager = $translationManager;
56 5
    }
57
58
    /**
59
     * @param Request $request
60
     * @return \Symfony\Component\HttpFoundation\Response
61
     */
62 1
    public function listAction(Request $request)
63
    {
64 1
        $translations = $this
65
            ->translationManager
66 1
            ->getTranslationList(
67 1
                $request->query->all()
68 1
            );
69
70 1
        if ($request->isXmlHttpRequest()) {
71 1
            $template = 'AsmTranslationLoaderBundle:Partial:list-tbl.html.twig';
72 1
        } else {
73 1
            $template = 'AsmTranslationLoaderBundle:Translation:list.html.twig';
74
        }
75
76 1
        return new Response(
77 1
            $this->templating->render(
78 1
                $template,
79
                array(
80 1
                    'translations' => $translations,
81
                )
82 1
            )
83 1
        );
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param string $locale
89
     * @param string $domain
90
     * @return \Symfony\Component\HttpFoundation\Response
91
     */
92 2
    public function formAction($key = '', $locale = '', $domain = '')
93
    {
94 2
        $action = 'asm_translation_loader.admin.update';
95 2
        $translation = $this->translationManager
96 2
            ->findTranslationBy(
97
                array(
98 2
                    'transKey' => $key,
99 2
                    'transLocale' => $locale,
100 2
                    'messageDomain' => $domain,
101
                )
102 2
            );
103
104 2
        if (empty($translation)) {
105 1
            $translation = new Translation();
106 1
            $action = 'asm_translation_loader.admin.create';
107 1
        }
108
109 2
        $form = $this->formFactory->create('asm_translation', $translation, array());
110
111 2
        return new Response(
112 2
            $this->templating->render(
113 2
                'AsmTranslationLoaderBundle:Translation:form.html.twig',
114
                array(
115 2
                    'form' => $form->createView(),
116 2
                    'action' => $action,
117
                )
118 2
            )
119 2
        );
120
    }
121
122
    /**
123
     * @param Request $request
124
     * @return \Symfony\Component\HttpFoundation\JsonResponse
125
     */
126 1
    public function createAction(Request $request)
127
    {
128 1
        return $this->handleForm('create', $request);
129
    }
130
131
    /**
132
     * @param Request $request
133
     * @return \Symfony\Component\HttpFoundation\JsonResponse
134
     */
135 1
    public function updateAction(Request $request)
136
    {
137 1
        return $this->handleForm('update', $request);
138
    }
139
140
    /**
141
     * @param Request $request
142
     * @return \Symfony\Component\HttpFoundation\JsonResponse
143
     */
144 2
    public function deleteAction(Request $request)
145
    {
146 2
        $error = array();
147 2
        $status = 200;
148
149 2
        $manager = $this->translationManager;
150 2
        $translation = $manager->findTranslationBy(
151
            array(
152 2
                'transKey' => $request->request->get('key'),
153 2
                'transLocale' => $request->request->get('locale'),
154 2
                'messageDomain' => $request->request->get('domain'),
155
            )
156 2
        );
157
158 2
        if (!empty($translation)) {
159 1
            $manager->removeTranslation($translation);
160 1
        } else {
161 1
            $error['message'] = 'translation not found ' . json_encode($request->request->all());
162 1
            $status = 404;
163
        }
164
165 2
        return new JsonResponse(
166 2
            array_merge(
167
                array(
168 2
                    'status' => $status,
169 2
                ),
170
                $error
171 2
            ),
172
            $status
173 2
        );
174
    }
175
176
    /**
177
     * @param string $type
178
     * @param Request $request
179
     * @return \Symfony\Component\HttpFoundation\Response
180
     */
181 4
    private function handleForm($type, $request)
182
    {
183 4
        $error = null;
184 4
        $form = $this->formFactory->create('asm_translation', new Translation(), array());
185 4
        $form->handleRequest($request);
186
187 4
        if ($form->isValid()) {
188 3
            $manager = $this->translationManager;
189
190 3
            if ('update' == $type) {
191
                /** @var \Asm\TranslationLoaderBundle\Entity\Translation $update */
192 1
                $update = $form->getData();
193
                // get translation from database again to keep date_created
194 1
                $translation = $manager->findTranslationBy(
195
                    array(
196 1
                        'transKey' => $update->getTransKey(),
197 1
                        'transLocale' => $update->getTransLocale(),
198 1
                        'messageDomain' => $update->getMessageDomain(),
199
                    )
200 1
                );
201
202
                $translation
203 1
                    ->setTransKey($update->getTransKey())
204 1
                    ->setTransLocale($update->getTransLocale())
205 1
                    ->setMessageDomain($update->getMessageDomain())
206 1
                    ->setTranslation($update->getTranslation());
207
208 1
                $manager->updateTranslation($translation);
209 1
            } else {
210 2
                $translation = $form->getData();
211 2
                $translation->setDateCreated(new \DateTime());
212
                try {
213 2
                    $manager->updateTranslation($translation);
214 1
                    $error = '';
215 2
                } catch (\Exception $e) {
216 1
                    $error = $e->getMessage();
217
                }
218
            }
219
220 3
            $response = new Response(
221 3
                $this->templating->render(
222 3
                    'AsmTranslationLoaderBundle:Translation:result.html.twig',
223
                    array(
224 3
                        'error' => $error,
225
                    )
226 3
                )
227 3
            );
228 3
        } else {
229 1
            $response = new Response(
230 1
                $this->templating->render(
231 1
                    'AsmTranslationLoaderBundle:Translation:form.html.twig',
232
                    array(
233 1
                        'form' => $form->createView(),
234
                    )
235 1
                )
236 1
            );
237
        }
238
239 4
        return $response;
240
    }
241
}
242