PhraseApp::export()   B
last analyzed

Complexity

Conditions 5
Paths 13

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
ccs 0
cts 16
cp 0
rs 8.439
cc 5
eloc 18
nc 13
nop 1
crap 30
1
<?php
2
3
namespace Translation\PlatformAdapter\PhraseApp;
4
5
use FAPI\PhraseApp\Model\Key\KeyCreated;
6
use FAPI\PhraseApp\Model\Key\KeySearchResults;
7
use FAPI\PhraseApp\Model\Translation\Index;
8
use FAPI\PhraseApp\PhraseAppClient;
9
use Symfony\Component\Translation\MessageCatalogueInterface;
10
use Translation\Common\Exception\StorageException;
11
use Translation\Common\Model\Message;
12
use Translation\Common\Storage;
13
use Translation\Common\TransferableStorage;
14
use Translation\SymfonyStorage\XliffConverter;
15
16
/**
17
 * @author Sascha-Oliver Prolic <[email protected]>
18
 */
19
class PhraseApp implements Storage, TransferableStorage
20
{
21
    /**
22
     * @var PhraseAppClient
23
     */
24
    private $client;
25
26
    /**
27
     * @var string
28
     */
29
    private $projectId;
30
31
    /**
32
     * @var array
33
     */
34
    private $localeToIdMapping;
35
36
    /**
37
     * @var array
38
     */
39
    private $domains;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $defaultLocale;
45
46 1
    public function __construct(
47
        PhraseAppClient $client,
48
        string $projectId,
49
        array $localeToIdMapping,
50
        array $domains,
51
        string $defaultLocale = null
52
    ) {
53 1
        $this->client = $client;
54 1
        $this->projectId = $projectId;
55 1
        $this->localeToIdMapping = $localeToIdMapping;
56 1
        $this->domains = $domains;
57 1
        $this->defaultLocale = $defaultLocale;
58 1
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function get($locale, $domain, $key)
64
    {
65
        /* @var Index $index */
66
        $index = $this->client->translation()->indexLocale($this->projectId, $this->getLocaleId($locale), [
67
            'tags' => $domain
68
        ]);
69
70
        foreach ($index as $translation) {
71
            if ($translation->getKey()->getName() === $domain.'::'.$key) {
72
                return new Message($key, $domain, $locale, $translation->getContent(), []);
73
            }
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function create(Message $message)
81
    {
82
        $localeId = $this->getLocaleId($message->getLocale());
83
84
        /* @var KeySearchResults $result */
85
        $result = $this->client->key()->search($this->projectId, [
86
            'tags' => $message->getDomain(),
87
            'name' => $message->getDomain().'::'.$message->getKey(),
88
        ]);
89
90 View Code Duplication
        foreach ($result as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
            if ($key->getName() === $message->getDomain().'::'.$message->getKey()) {
92
                /* @var Index $index */
93
                $index = $this->client->translation()->indexKey($this->projectId, $key->getId(), ['tags' => $message->getDomain()]);
94
                foreach ($index as $translation) {
95
                    if ($translation->getLocale()->getId() === $localeId) {
96
                        // Translation does already exist
97
                        return;
98
                    }
99
                }
100
101
                // Create a translation with an existing key
102
                $this->client->translation()->create($this->projectId, $localeId, $key->getId(), $message->getTranslation());
103
104
                return;
105
            }
106
        }
107
108
        try {
109
            /* @var KeyCreated $keyCreated */
110
            $keyCreated = $this->client->key()->create($this->projectId, $message->getDomain().'::'.$message->getKey(), [
111
                'tags' => $message->getDomain(),
112
            ]);
113
        } catch (UnprocessableEntityException $e) {
0 ignored issues
show
Bug introduced by
The class Translation\PlatformAdap...cessableEntityException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
114
            // Translaton does already exist
115
            return;
116
        }
117
118
        $this->client->translation()->create(
119
            $this->projectId,
120
            $this->getLocaleId($message->getLocale()),
121
            $keyCreated->getId(),
122
            $message->getTranslation()
123
        );
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function update(Message $message)
130
    {
131
        $localeId = $this->getLocaleId($message->getLocale());
132
        /* @var KeySearchResults $results */
133
        $results = $this->client->key()->search($this->projectId, [
134
            'tags' => $message->getDomain(),
135
            'name' => $message->getDomain().'::'.$message->getKey()
136
        ]);
137
138 View Code Duplication
        foreach ($results as $searchResult) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            if ($searchResult->getName() === $message->getDomain().'::'.$message->getKey()) {
140
141
                /* @var Index $translations */
142
                $translations = $this->client->translation()->indexKey($this->projectId, $searchResult->getId(), [
143
                    'tags' => $message->getDomain(),
144
                ]);
145
146
                foreach ($translations as $translation) {
147
                    if ($translation->getLocale()->getId() === $localeId) {
148
                        $this->client->translation()->update(
149
                            $this->projectId,
150
                            $translation->getId(),
151
                            $message->getTranslation()
152
                        );
153
154
                        return;
155
                    }
156
                }
157
            }
158
        }
159
160
        // No translation was found, lets create one.
161
        $this->create($message);
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function delete($locale, $domain, $key)
168
    {
169
        /* @var KeySearchResults $results */
170
        $results = $this->client->key()->search($this->projectId, $this->getLocaleId($locale), [
0 ignored issues
show
Documentation introduced by
$this->getLocaleId($locale) is of type string, but the function expects a array.

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...
Unused Code introduced by
The call to Key::search() has too many arguments starting with array('tags' => $domain,... $domain . '::' . $key).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
171
            'tags' => $domain,
172
            'name' => $domain.'::'.$key
173
        ]);
174
175
        foreach ($results as $searchResult) {
176
            if ($searchResult->getName() === $domain.'::'.$key) {
177
                $this->client->key()->delete($this->projectId, $searchResult->getId());
178
                break;
179
            }
180
        }
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function export(MessageCatalogueInterface $catalogue)
187
    {
188
        $locale = $catalogue->getLocale();
189
        $localeId = $this->getLocaleId($locale);
190
191
        foreach ($this->domains as $domain) {
192
            try {
193
                $response = $this->client->locale()->download($this->projectId, $localeId, 'symfony_xliff', [
194
                    'tag' => $domain
195
                ]);
196
            } catch (\Throwable $e) {
197
                throw new StorageException($e->getMessage());
198
            }
199
200
            try {
201
                $newCatalogue = XliffConverter::contentToCatalogue($response, $locale, $domain);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $this->client->locale()-...rray('tag' => $domain)) on line 193 can also be of type object<Psr\Http\Message\ResponseInterface>; however, Translation\SymfonyStora...r::contentToCatalogue() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
202
203
                $messages = [];
204
205
                foreach ($newCatalogue->all($domain) as $message => $translation) {
206
                    $messages[substr($message, strlen($domain) + 2)] = $translation;
207
                }
208
209
                $newCatalogue->replace($messages, $domain);
210
211
                $catalogue->addCatalogue($newCatalogue);
212
            } catch (\Throwable $e) {
213
                // ignore empty translation files
214
            }
215
        }
216
217
        return $catalogue;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function import(MessageCatalogueInterface $catalogue)
224
    {
225
        $locale = $catalogue->getLocale();
226
        $localeId = $this->getLocaleId($locale);
227
228
        foreach ($this->domains as $domain) {
229
            $messages = [];
230
231
            foreach ($catalogue->all($domain) as $message => $translation) {
232
                $messages[$domain . '::' . $message] = $translation;
233
            }
234
235
            $catalogue->replace($messages, $domain);
236
237
            if ($this->defaultLocale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->defaultLocale of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
238
                $options = ['default_locale' => $this->defaultLocale];
239
            } else {
240
                $options = [];
241
            }
242
243
            $data = XliffConverter::catalogueToContent($catalogue, $domain, $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...
244
245
            $file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf';
246
247
            try {
248
                file_put_contents($file, $data);
249
250
                $this->client->upload()->upload($this->projectId, 'symfony_xliff', $file, [
251
                    'locale_id' => $localeId,
252
                    'tags' => $domain,
253
                ]);
254
            } catch (\Throwable $e) {
255
                throw new StorageException($e->getMessage());
256
            } finally {
257
                unlink($file);
258
            }
259
        }
260
    }
261
262
    /**
263
     * @param string $locale
264
     *
265
     * @return string
266
     *
267
     * @throws StorageException If no id was found for locale.
268
     */
269
    private function getLocaleId(string $locale): string
270
    {
271
        if (isset($this->localeToIdMapping[$locale])) {
272
            return $this->localeToIdMapping[$locale];
273
        }
274
275
        throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale));
276
    }
277
}
278