Completed
Pull Request — master (#80)
by
unknown
64:19
created

TranslationController::addTagAction()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 27
rs 8.8571
cc 3
eloc 20
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
use Symfony\Component\Console\Input\ArrayInput;
21
use Symfony\Component\Console\Output\NullOutput;
22
23
/**
24
 * Controller used for working with individual translation
25
 * in edit view
26
 */
27
class TranslationController extends Controller
28
{
29
    /**
30
     * @var Repository
31
     */
32
    private $repository;
33
34
    /**
35
     * Injects elasticsearch repository for listing actions.
36
     *
37
     * @param Repository $repository Elasticsearch repository.
38
     */
39
    public function __construct(Repository $repository)
40
    {
41
        $this->repository = $repository;
42
    }
43
    /**
44
     * Add a tag action
45
     *
46
     * @param Request $request
47
     *
48
     * @return Response
49
     */
50
    public function addTagAction(Request $request)
51
    {
52
        $response = [];
53
        $translation = $this->repository->find($request->request->get('id'));
54
        $cache = $this->get('es.cache_engine');
55
        $requestHandler = $this->get('ongr_translations.request_handler');
56
        $requestHandler->setTranslation($translation);
57
        try {
58
            $this->get('ongr_translations.translation_manager')
59
                ->add($requestHandler->remakeRequest($request));
60
        } catch (\Exception $e) {
61
            $response['error'] = $e->getMessage();
62
        }
63
        !isset($response['error']) ?
64
            $response['success'] = 'Tag successfully added' :
65
            $response['success'] = false;
66
        $cache->save('translations_edit', $response);
67
        return new RedirectResponse(
68
            $this->generateUrl(
69
                'ongr_translations_translation_page',
70
                [
71
                    'translation' => $translation->getKey(),
72
                    'domain' => $translation->getDomain(),
73
                ]
74
            )
75
        );
76
    }
77
78
    /**
79
     * Add a tag action
80
     *
81
     * @param Request $request
82
     *
83
     * @return Response
84
     */
85
    public function editAction(Request $request)
86
    {
87
        $response = [];
88
        $translation = $this->repository->find($request->request->get('id'));
89
        $cache = $this->get('es.cache_engine');
90
        $requestHandler = $this->get('ongr_translations.request_handler');
91
        $requestHandler->setTranslation($translation);
92
        try {
93
            $requests = $requestHandler->remakeRequest($request);
94
            if (empty($requests)) {
95
                throw new \InvalidArgumentException('No new message values given');
96
            }
97
            foreach ($requests as $messageRequest) {
98
                $this->get('ongr_translations.translation_manager')
99
                    ->edit($messageRequest);
100
            }
101
        } catch (\Exception $e) {
102
            $response['error'] = $e->getMessage();
103
        }
104
        !isset($response['error']) ?
105
            $response['success'] = 'Messages updated successfully' :
106
            $response['success'] = false;
107
        $cache->save('translations_edit', $response);
108
        return new RedirectResponse(
109
            $this->generateUrl(
110
                'ongr_translations_translation_page',
111
                [
112
                    'translation' => $translation->getKey(),
113
                    'domain' => $translation->getDomain(),
114
                ]
115
            )
116
        );
117
    }
118
119
    /**
120
     * Exports the messages with 'dirty' status
121
     */
122
    public function exportAction()
123
    {
124
        $response = [];
125
        $cache = $this->get('es.cache_engine');
126
        $cwd = getcwd();
127 View Code Duplication
        if (substr($cwd, -3) === 'web') {
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...
128
            chdir($cwd . DIRECTORY_SEPARATOR . '..');
129
        }
130
        try {
131
            $this->get('ongr_translations.command.export')
132
                ->run(new ArrayInput([]), new NullOutput());
133
        } catch (\Exception $e) {
134
            $response['error'] = $e->getMessage();
135
        }
136
        !isset($response['error']) ?
137
            $response['success'] = 'Messages exported successfully' :
138
            $response['success'] = false;
139
        $cache->save('translations_edit', $response);
140
        return new RedirectResponse(
141
            $this->generateUrl('ongr_translations_export_page')
142
        );
143
    }
144
}
145