Completed
Push — master ( b4c0e6...aeda72 )
by Damien
15:01
created

EditInPlaceController::rebuildTranslations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 12
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\Model\EditInPlaceMessage;
20
use Translation\Bundle\Service\StorageService;
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
    public function editAction(Request $request, $configName, $locale)
35
    {
36
        try {
37
            $messages = $this->getMessages($request, ['Edit']);
38
        } catch (MessageValidationException $e) {
39
            return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST);
40
        }
41
42
        /** @var StorageService $storage */
43
        $storage = $this->get('php_translation.storage.'.$configName);
44
        foreach ($messages as $message) {
45
            $storage->update($message->convertToMessage($locale));
46
        }
47
48
        $this->rebuildTranslations($locale);
49
50
        return new Response();
51
    }
52
53
    /**
54
     * Remove the Symfony translation cache and warm it up again.
55
     *
56
     * @param $locale
57
     */
58
    private function rebuildTranslations($locale)
59
    {
60
        $cacheDir = $this->getParameter('kernel.cache_dir');
61
        $translationDir = sprintf('%s/translations', $cacheDir);
62
63
        $filesystem = $this->get('filesystem');
64
        $finder = new Finder();
65
66
        if (!is_writable($translationDir)) {
67
            throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $translationDir));
68
        }
69
70
        // Remove the translations for this locale
71
        $files = $finder->files()->name('*.'.$locale.'.*')->in($translationDir);
72
        foreach ($files as $file) {
73
            $filesystem->remove($file);
74
        }
75
76
        // Build them again
77
        $this->get('translator')->warmUp();
78
    }
79
80
    /**
81
     * Get and validate messages from the request.
82
     *
83
     * @param Request $request
84
     * @param array   $validationGroups
85
     *
86
     * @return EditInPlaceMessage[]
87
     *
88
     * @throws MessageValidationException
89
     */
90
    private function getMessages(Request $request, array $validationGroups = [])
91
    {
92
        $json = $request->getContent();
93
        $data = json_decode($json, true);
94
        $messages = [];
95
        $validator = $this->get('validator');
96
97
        foreach ($data as $key => $value) {
98
            list($domain, $translationKey) = explode('|', $key);
99
100
            $message = new EditInPlaceMessage();
101
            $message->setKey($translationKey);
102
            $message->setMessage($value);
103
            $message->setDomain($domain);
104
105
            $errors = $validator->validate($message, null, $validationGroups);
106
            if (count($errors) > 0) {
107
                throw MessageValidationException::create();
108
            }
109
110
            $messages[] = $message;
111
        }
112
113
        return $messages;
114
    }
115
}
116