Completed
Push — master ( 8f81c9...db13bb )
by Olivier
03:10 queued 13s
created

FileStorage::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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\Component\Translation\MessageCatalogue;
15
use Symfony\Component\Translation\MessageCatalogueInterface;
16
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
17
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
18
use Translation\Common\Model\Message;
19
use Translation\Common\Model\MessageInterface;
20
use Translation\Common\Storage;
21
use Translation\Common\TransferableStorage;
22
23
/**
24
 * This storage uses Symfony's writer and loader.
25
 *
26
 * @author Tobias Nyholm <[email protected]>
27
 */
28
final class FileStorage implements Storage, TransferableStorage
29
{
30
    /**
31
     * @var TranslationWriterInterface
32
     */
33
    private $writer;
34
35
    /**
36
     * @var TranslationReaderInterface
37
     */
38
    private $reader;
39
40
    /**
41
     * @var array directory path
42
     */
43
    private $dir;
44
45
    /**
46
     * @var array with option to the dumper
47
     */
48
    private $options;
49
50
    /**
51
     * @var MessageCatalogue[] Fetched catalogues
52
     */
53
    private $catalogues;
54
55
    /**
56
     * @param TranslationWriterInterface $writer
57
     * @param TranslationReaderInterface $reader
58
     * @param array                      $dir
59
     * @param array                      $options
60
     */
61 9
    public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, array $dir, array $options = [])
62
    {
63 9
        if (empty($dir)) {
64 1
            throw new \LogicException('Third parameter of FileStorage cannot be empty');
65
        }
66
67 8
        if (!array_key_exists('xliff_version', $options)) {
68
            // Set default value for xliff version.
69 8
            $options['xliff_version'] = '2.0';
70
        }
71
72 8
        $this->writer = $writer;
73 8
        $this->reader = $reader;
74 8
        $this->dir = $dir;
75 8
        $this->options = $options;
76 8
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 1
    public function get($locale, $domain, $key)
82
    {
83 1
        $catalogue = $this->getCatalogue($locale);
84 1
        $translation = $catalogue->get($key, $domain);
85
86 1
        return new Message($key, $domain, $locale, $translation);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function create(MessageInterface $m)
93
    {
94 2
        $catalogue = $this->getCatalogue($m->getLocale());
95 2
        if (!$catalogue->defines($m->getKey(), $m->getDomain())) {
96 2
            $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
97 2
            $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
98
        }
99 2
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function update(MessageInterface $m)
105
    {
106 1
        $catalogue = $this->getCatalogue($m->getLocale());
107 1
        $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
108 1
        $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
109 1
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function delete($locale, $domain, $key)
115
    {
116 1
        $catalogue = $this->getCatalogue($locale);
117 1
        $messages = $catalogue->all($domain);
118 1
        unset($messages[$key]);
119
120 1
        $catalogue->replace($messages, $domain);
121 1
        $this->writeCatalogue($catalogue, $locale, $domain);
122 1
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 1
    public function export(MessageCatalogueInterface $catalogue)
128
    {
129 1
        $locale = $catalogue->getLocale();
130 1
        $catalogue->addCatalogue($this->getCatalogue($locale));
0 ignored issues
show
Documentation introduced by
$this->getCatalogue($locale) is of type object<Symfony\Component...ation\MessageCatalogue>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131 1
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function import(MessageCatalogueInterface $catalogue)
137
    {
138 1
        $domains = $catalogue->getDomains();
139 1
        foreach ($domains as $domain) {
140 1
            $this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain);
141
        }
142 1
    }
143
144
    /**
145
     * Save catalogue back to file.
146
     *
147
     * @param MessageCatalogueInterface $catalogue
148
     * @param string                    $domain
149
     */
150 5
    private function writeCatalogue(MessageCatalogueInterface $catalogue, $locale, $domain)
151
    {
152 5
        $resources = $catalogue->getResources();
153 5
        $options = $this->options;
154 5
        $written = false;
155 5
        foreach ($resources as $resource) {
156 3
            $path = (string) $resource;
157 3
            if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
158 3
                $options['path'] = str_replace($matches[0], '', $path);
159 3
                $this->writer->write($catalogue, $matches[1], $options);
0 ignored issues
show
Compatibility introduced by
$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...
160 3
                $written = true;
161
            }
162
        }
163
164 5
        if ($written) {
165
            // We have written the translation to a file.
166 3
            return;
167
        }
168
169 2
        $options['path'] = reset($this->dir);
170 2
        $format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
171 2
        $this->writer->write($catalogue, $format, $options);
0 ignored issues
show
Compatibility introduced by
$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...
172 2
    }
173
174
    /**
175
     * @param string $locale
176
     *
177
     * @return MessageCatalogue
178
     */
179 6
    private function getCatalogue($locale)
180
    {
181 6
        if (empty($this->catalogues[$locale])) {
182 6
            $this->loadCatalogue($locale, $this->dir);
183
        }
184
185 6
        return $this->catalogues[$locale];
186
    }
187
188
    /**
189
     * Load catalogue from files.
190
     *
191
     * @param string $locale
192
     * @param array  $dirs
193
     */
194 6
    private function loadCatalogue($locale, array $dirs)
195
    {
196 6
        $currentCatalogue = new MessageCatalogue($locale);
197 6
        foreach ($dirs as $path) {
198 6
            if (is_dir($path)) {
199 5
                $this->reader->read($path, $currentCatalogue);
200
            }
201
        }
202
203 6
        $this->catalogues[$locale] = $currentCatalogue;
204 6
    }
205
}
206