Completed
Push — master ( 75ac31...11edf4 )
by Tobias
08:15 queued 06:58
created

src/FileStorage.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\SymfonyStorage;
13
14
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader;
15
use Symfony\Component\Translation\MessageCatalogue;
16
use Symfony\Component\Translation\MessageCatalogueInterface;
17
use Symfony\Component\Translation\Writer\TranslationWriter;
18
use Translation\Common\Model\Message;
19
use Translation\Common\Storage;
20
use Translation\Common\TransferableStorage;
21
22
/**
23
 * This storage uses Symfony's writer and loader.
24
 *
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
final class FileStorage implements Storage, TransferableStorage
28
{
29
    /**
30
     * @var TranslationWriter
31
     */
32
    private $writer;
33
34
    /**
35
     * @var TranslationLoader|SymfonyTranslationLoader
36
     */
37
    private $loader;
38
39
    /**
40
     * @var array directory path
41
     */
42
    private $dir;
43
44
    /**
45
     * @var array with option to the dumper
46
     */
47
    private $options;
48
49
    /**
50
     * @var MessageCatalogue[] Fetched catalogies
51
     */
52
    private $catalogues;
53
54
    /**
55
     * @param TranslationWriter                          $writer
56
     * @param SymfonyTranslationLoader|TranslationLoader $loader
57
     * @param array                                      $dir
58
     * @param array                                      $options
59
     */
60 10
    public function __construct(TranslationWriter $writer, $loader, array $dir, array $options = [])
61
    {
62 10
        if (!$loader instanceof SymfonyTranslationLoader && !$loader instanceof TranslationLoader) {
63 1
            throw new \LogicException('Second parameter of FileStorage must be a Symfony translation loader or implement Translation\SymfonyStorage\TranslationLoader');
64
        }
65
66 9
        if (empty($dir)) {
67 1
            throw new \LogicException('Third parameter of FileStorage cannot be empty');
68
        }
69
70 8
        if (!array_key_exists('xliff_version', $options)) {
71
            // Set default value for xliff version.
72 8
            $options['xliff_version'] = '2.0';
73 8
        }
74
75 8
        $this->writer = $writer;
76 8
        $this->loader = $loader;
77 8
        $this->dir = $dir;
78 8
        $this->options = $options;
79 8
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 1
    public function get($locale, $domain, $key)
85
    {
86 1
        $catalogue = $this->getCatalogue($locale);
87 1
        $translation = $catalogue->get($key, $domain);
88
89 1
        return new Message($key, $domain, $locale, $translation);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 2
    public function create(Message $m)
96
    {
97 2
        $catalogue = $this->getCatalogue($m->getLocale());
98 2
        if (!$catalogue->defines($m->getKey(), $m->getDomain())) {
99 2
            $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
100 2
            $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
101 2
        }
102 2
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 1
    public function update(Message $m)
108
    {
109 1
        $catalogue = $this->getCatalogue($m->getLocale());
110 1
        $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
111 1
        $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
112 1
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 1
    public function delete($locale, $domain, $key)
118
    {
119 1
        $catalogue = $this->getCatalogue($locale);
120 1
        $messages = $catalogue->all($domain);
121 1
        unset($messages[$key]);
122
123 1
        $catalogue->replace($messages, $domain);
124 1
        $this->writeCatalogue($catalogue, $locale, $domain);
125 1
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 1
    public function export(MessageCatalogueInterface $catalogue)
131
    {
132 1
        $locale = $catalogue->getLocale();
133 1
        $catalogue->addCatalogue($this->getCatalogue($locale));
134 1
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 1
    public function import(MessageCatalogueInterface $catalogue)
140
    {
141 1
        $domains = $catalogue->getDomains();
142 1
        foreach ($domains as $domain) {
143 1
            $this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain);
0 ignored issues
show
$catalogue of type object<Symfony\Component...sageCatalogueInterface> is not a sub-type of object<Symfony\Component...ation\MessageCatalogue>. It seems like you assume a concrete implementation of the interface Symfony\Component\Transl...ssageCatalogueInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
144 1
        }
145 1
    }
146
147
    /**
148
     * Save catalogue back to file.
149
     *
150
     * @param MessageCatalogue $catalogue
151
     * @param string           $domain
152
     */
153 5
    private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain)
154
    {
155 5
        $resources = $catalogue->getResources();
156 5
        $options = $this->options;
157 5
        $written = false;
158 5
        foreach ($resources as $resource) {
159 3
            $path = (string) $resource;
160 3
            if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
161 3
                $options['path'] = str_replace($matches[0], '', $path);
162 3
                $this->writer->writeTranslations($catalogue, $matches[1], $options);
163 3
                $written = true;
164 3
            }
165 5
        }
166
167 5
        if ($written) {
168
            // We have written the translation to a file.
169 3
            return;
170
        }
171
172 2
        $options['path'] = reset($this->dir);
173 2
        $format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
174 2
        $this->writer->writeTranslations($catalogue, $format, $options);
175 2
    }
176
177
    /**
178
     * @param string $locale
179
     *
180
     * @return MessageCatalogue
181
     */
182 6
    private function getCatalogue($locale)
183
    {
184 6
        if (empty($this->catalogues[$locale])) {
185 6
            $this->loadCatalogue($locale, $this->dir);
186 6
        }
187
188 6
        return $this->catalogues[$locale];
189
    }
190
191
    /**
192
     * Load catalogue from files.
193
     *
194
     * @param string $locale
195
     * @param array  $dirs
196
     */
197 6
    private function loadCatalogue($locale, array $dirs)
198
    {
199 6
        $currentCatalogue = new MessageCatalogue($locale);
200 6
        foreach ($dirs as $path) {
201 6
            if (is_dir($path)) {
202 5
                $this->loader->loadMessages($path, $currentCatalogue);
203 5
            }
204 6
        }
205
206 6
        $this->catalogues[$locale] = $currentCatalogue;
207 6
    }
208
}
209