Completed
Push — master ( 584a71...a5bdca )
by Tobias
24:17 queued 18:42
created

EditInPlaceController::editAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 3
crap 3
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\Controller;
13
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Translation\Bundle\Exception\MessageValidationException;
19
use Translation\Bundle\Service\StorageService;
20
use Translation\Common\Model\Message;
21
22
/**
23
 * @author Damien Alexandre <[email protected]>
24
 */
25
class EditInPlaceController extends Controller
26
{
27
    /**
28
     * @param Request $request
29
     * @param string  $configName
30
     * @param string  $locale
31
     *
32
     * @return Response
33
     */
34 2
    public function editAction(Request $request, $configName, $locale)
35
    {
36
        try {
37 2
            $messages = $this->getMessages($request, $locale, ['Edit']);
38 2
        } catch (MessageValidationException $e) {
39 1
            return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
40
        }
41
42
        /** @var StorageService $storage */
43 1
        $storage = $this->get('php_translation.storage.'.$configName);
44 1
        foreach ($messages as $message) {
45 1
            $storage->update($message);
46 1
        }
47
48 1
        $this->rebuildTranslations($locale);
49
50 1
        return new Response();
51
    }
52
53
    /**
54
     * Remove the Symfony translation cache and warm it up again.
55
     *
56
     * @param $locale
57
     */
58 1
    private function rebuildTranslations($locale)
59
    {
60 1
        $cacheDir = $this->getParameter('kernel.cache_dir');
61 1
        $translationDir = sprintf('%s/translations', $cacheDir);
62
63 1
        $filesystem = $this->get('filesystem');
64 1
        $finder = new Finder();
65
66 1
        if (!is_dir($translationDir)) {
67 1
            mkdir($translationDir);
68 1
        }
69
70 1
        if (!is_writable($translationDir)) {
71
            throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $translationDir));
72
        }
73
74
        // Remove the translations for this locale
75 1
        $files = $finder->files()->name('*.'.$locale.'.*')->in($translationDir);
76 1
        foreach ($files as $file) {
77
            $filesystem->remove($file);
78 1
        }
79
80
        // Build them again
81 1
        $translator = $this->get('translator');
82 1
        if (method_exists($translator, 'warmUp')) {
83
            $translator->warmUp($translationDir);
84
        }
85 1
    }
86
87
    /**
88
     * Get and validate messages from the request.
89
     *
90
     * @param Request $request
91
     * @param string  $locale
92
     * @param array   $validationGroups
93
     *
94
     * @return Message[]
95
     *
96
     * @throws MessageValidationException
97
     */
98 2
    private function getMessages(Request $request, $locale, array $validationGroups = [])
99
    {
100 2
        $json = $request->getContent();
101 2
        $data = json_decode($json, true);
102 2
        $messages = [];
103 2
        $validator = $this->get('validator');
104
105 2
        foreach ($data as $key => $value) {
106 2
            list($domain, $translationKey) = explode('|', $key);
107
108 2
            $message = new Message();
109 2
            $message->setKey($translationKey);
110 2
            $message->setTranslation($value);
111 2
            $message->setDomain($domain);
112 2
            $message->setLocale($locale);
113
114 2
            $errors = $validator->validate($message, null, $validationGroups);
115 2
            if (count($errors) > 0) {
116 1
                throw MessageValidationException::create();
117
            }
118
119 2
            $messages[] = $message;
120 2
        }
121
122 1
        return $messages;
123
    }
124
}
125