FilesystemUpdater::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 5
crap 1
1
<?php
2
3
namespace Happyr\TranslationBundle\Translation;
4
5
use Happyr\TranslationBundle\Model\Message;
6
use Symfony\Component\EventDispatcher\Event;
7
use Symfony\Component\Translation\Dumper\DumperInterface;
8
use Symfony\Component\Translation\Exception\NotFoundResourceException;
9
use Symfony\Component\Translation\Loader\LoaderInterface;
10
use Symfony\Component\Translation\MessageCatalogue;
11
12
/**
13
 * @author Tobias Nyholm, Damien Harper
14
 *
15
 * Update the locale file system with changes in the catalogue
16
 */
17
class FilesystemUpdater
18
{
19
    /**
20
     * @var LoaderInterface loader
21
     */
22
    private $loader;
23
24
    /**
25
     * @var DumperInterface dumper
26
     */
27
    private $dumper;
28
29
    /**
30
     * @var string targetDir
31
     */
32
    private $targetDir;
33
34
    /**
35
     * @var string fileExtension
36
     */
37
    private $fileExtension;
38
39
    /**
40
     * @var Message[] messages
41
     */
42
    private $messages;
43
44
    /**
45
     * @var bool
46
     */
47
    private $syncEmptyTranslations;
48
49
    /**
50
     * Filesystem constructor.
51
     *
52
     * @param LoaderInterface $loader
53
     * @param DumperInterface $dumper
54
     * @param string          $targetDir
55
     * @param string          $fileExtension
56
     * @param bool            $syncEmptyTranslations
57
     */
58 1
    public function __construct(LoaderInterface $loader, DumperInterface $dumper, $targetDir, $fileExtension, $syncEmptyTranslations = true)
59
    {
60 1
        $this->loader = $loader;
61 1
        $this->dumper = $dumper;
62 1
        $this->targetDir = $targetDir;
63 1
        $this->messages = array();
64 1
        $this->fileExtension = $fileExtension;
65 1
        $this->syncEmptyTranslations = $syncEmptyTranslations;
66 1
    }
67
68
    /**
69
     * Returns translation file type.
70
     *
71
     * @return string
72
     */
73
    public function getFileExtension()
74
    {
75
        return $this->fileExtension;
76
    }
77
78
    /**
79
     * @param string $fileName
80
     * @param string $data     the file contents
81
     */
82
    public function writeToFile($fileName, $data)
83
    {
84
        if (!is_dir($this->targetDir)) {
85
            mkdir($this->targetDir, 0777, true);
86
        }
87
88
        file_put_contents(sprintf('%s/%s', $this->targetDir, $fileName), $data);
89
    }
90
91
    /**
92
     * Update message catalogues.
93
     *
94
     * @param Message[] $messages
95
     */
96
    public function updateMessageCatalog(array $messages)
97
    {
98
        $this->messages = array_merge($messages, $this->messages);
99
    }
100
101
    /**
102
     * Update the file system after the Response has been sent back to the client.
103
     *
104
     * @param Event $event
105
     *
106
     * @throws \ErrorException
107
     * @throws \Exception
108
     */
109
    public function onTerminate(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        if (empty($this->messages)) {
112
            return;
113
        }
114
115
        /** @var MessageCatalogue[] $catalogues */
116
        $catalogues = array();
117
        foreach ($this->messages as $m) {
118
            $key = $m->getLocale().$m->getDomain();
119
            if (!isset($catalogues[$key])) {
120
                $file = sprintf('%s/%s.%s.%s', $this->targetDir, $m->getDomain(), $m->getLocale(), $this->getFileExtension());
121
                
122
                try {
123
                    $catalogues[$key] = $this->loader->load($file, $m->getLocale(), $m->getDomain());
124
                } catch (NotFoundResourceException $e) {
125
                    $catalogues[$key] = new MessageCatalogue($m->getLocale());
126
                }
127
            }
128
129
            $translation = $m->getTranslation();
130
            if (empty($translation)) {
131
                if ($this->syncEmptyTranslations) {
132
                    $translation = sprintf('[%s]', $m->getId());
133
                } else {
134
                    continue;
135
                }
136
            }
137
138
            $catalogues[$key]->set($m->getId(), $translation, $m->getDomain());
139
        }
140
141
        foreach ($catalogues as $catalogue) {
142
            try {
143
                $this->dumper->dump($catalogue, ['path' => $this->targetDir]);
144
            } catch (\ErrorException $e) {
145
                // Could not save file
146
                // TODO better error handling
147
                throw $e;
148
            }
149
        }
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getTargetDir()
156
    {
157
        return $this->targetDir;
158
    }
159
}
160