Completed
Pull Request — master (#80)
by
unknown
123:09 queued 58:07
created

TranslationController::addTagAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 3
eloc 19
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\Controller;
13
14
use ONGR\ElasticsearchBundle\Service\Repository;
15
use ONGR\FilterManagerBundle\Filter\ViewData;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Response;
20
21
/**
22
 * Controller used for working with individual translation
23
 * in edit view
24
 */
25
class TranslationController extends Controller
26
{
27
    /**
28
     * @var Repository
29
     */
30
    private $repository;
31
32
    /**
33
     * Injects elasticsearch repository for listing actions.
34
     *
35
     * @param Repository $repository Elasticsearch repository.
36
     */
37
    public function __construct(Repository $repository)
38
    {
39
        $this->repository = $repository;
40
    }
41
    /**
42
     * Add a tag action
43
     *
44
     * @param Request $request
45
     *
46
     * @return Response
47
     */
48
    public function addTagAction(Request $request)
49
    {
50
        $response = [];
51
        $translation = $this->repository->find($request->request->get('id'));
52
        $cache = $this->get('es.cache_engine');
53
        $requestHandler = $this->get('ongr_translations.request_handler');
54
        try {
55
            $this->get('ongr_translations.translation_manager')
56
                ->add($requestHandler->remakeRequest($request));
57
        } catch (\Exception $e) {
58
            $response['error'] = $e->getMessage();
59
        }
60
        !isset($response['error']) ?
61
            $response['success'] = 'Tag successfully added' :
62
            $response['success'] = false;
63
        $cache->save('translations_edit', $response);
64
        return new RedirectResponse(
65
            $this->generateUrl(
66
                'ongr_translations_translation_page',
67
                [
68
                    'translation' => $translation->getKey(),
69
                    'domain' => $translation->getDomain(),
70
                ]
71
            )
72
        );
73
    }
74
75
    /**
76
     * Add a tag action
77
     *
78
     * @param Request $request
79
     *
80
     * @return Response
81
     */
82
    public function editAction(Request $request)
83
    {
84
        $response = [];
85
        $translation = $this->repository->find($request->request->get('id'));
86
        $cache = $this->get('es.cache_engine');
87
        $requestHandler = $this->get('ongr_translations.request_handler');
88
        $requests = $requestHandler->remakeRequest($request);
89
        try {
90
            foreach ($requests as $messageRequest) {
91
                $this->get('ongr_translations.translation_manager')
92
                    ->edit($messageRequest);
93
            }
94
        } catch (\Exception $e) {
95
            $response['error'] = $e->getMessage();
96
        }
97
        !isset($response['error']) ?
98
            $response['success'] = 'Messages updated successfully' :
99
            $response['success'] = false;
100
        $cache->save('translations_edit', $response);
101
        return new RedirectResponse(
102
            $this->generateUrl(
103
                'ongr_translations_translation_page',
104
                [
105
                    'translation' => $translation->getKey(),
106
                    'domain' => $translation->getDomain(),
107
                ]
108
            )
109
        );
110
    }
111
}
112