Completed
Push — master ( 1397e0...6a2cd6 )
by Sascha-Oliver
9s
created

src/PhraseApp.php (3 issues)

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
namespace Translation\PlatformAdapter\PhraseApp;
4
5
use FAPI\PhraseApp\PhraseAppClient;
6
use Symfony\Component\Translation\MessageCatalogueInterface;
7
use Translation\Common\Exception\StorageException;
8
use Translation\Common\Model\Message;
9
use Translation\Common\Storage;
10
use Translation\Common\TransferableStorage;
11
use Translation\PlatformAdapter\PhraseApp\Bridge\Symfony\XliffConverter;
12
13
/**
14
 * @author Sascha-Oliver Prolic <[email protected]>
15
 */
16
class PhraseApp implements Storage, TransferableStorage
17
{
18
    /**
19
     * @var PhraseAppClient
20
     */
21
    private $client;
22
23
    /**
24
     * @var string
25
     */
26
    private $projectId;
27
28
    /**
29
     * @var array
30
     */
31
    private $localeToIdMapping;
32
33
    /**
34
     * @var array
35
     */
36
    private $domains;
37
38
    /**
39
     * @var string|null
40
     */
41
    private $defaultLocale;
42
43
    public function __construct(
44
        PhraseAppClient $client,
45
        string $projectId,
46
        array $localeToIdMapping,
47
        array $domains,
48
        string $defaultLocale = null
49
    ) {
50
        $this->client = $client;
51
        $this->projectId = $projectId;
52
        $this->localeToIdMapping = $localeToIdMapping;
53
        $this->domains = $domains;
54
        $this->defaultLocale = $defaultLocale;
55
    }
56
57
    public function get($locale, $domain, $key)
58
    {
59
        throw new \BadMethodCallException('Not implemented');
60
    }
61
62
    public function create(Message $message)
63
    {
64
        throw new \BadMethodCallException('Not implemented');
65
    }
66
67
    public function update(Message $message)
68
    {
69
        throw new \BadMethodCallException('Not implemented');
70
    }
71
72
    public function delete($locale, $domain, $key)
73
    {
74
        throw new \BadMethodCallException('Not implemented');
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function export(MessageCatalogueInterface $catalogue)
81
    {
82
        $locale = $catalogue->getLocale();
83
        $localeId = $this->getLocaleId($locale);
84
85
        foreach ($this->domains as $domain) {
86
            try {
87
                $response = $this->client->export()->locale($this->projectId, $localeId, 'symfony_xliff', [
88
                    'tag' => $domain
89
                ]);
90
            } catch (\Throwable $e) {
91
                throw new StorageException($e->getMessage());
92
            }
93
94
            try {
95
                $newCatalogue = XliffConverter::contentToCatalogue($response, $locale, $domain);
0 ignored issues
show
It seems like $response defined by $this->client->export()-...rray('tag' => $domain)) on line 87 can also be of type object<Psr\Http\Message\ResponseInterface>; however, Translation\PlatformAdap...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...
96
97
                $messages = [];
98
99
                foreach ($newCatalogue->all($domain) as $message => $translation) {
100
                    $messages[substr($message, strlen($domain) + 2)] = $translation;
101
                }
102
103
                $newCatalogue->replace($messages, $domain);
104
105
                $catalogue->addCatalogue($newCatalogue);
106
            } catch (\Throwable $e) {
107
                // ignore empty translation files
108
            }
109
        }
110
111
        return $catalogue;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function import(MessageCatalogueInterface $catalogue)
118
    {
119
        $locale = $catalogue->getLocale();
120
        $localeId = $this->getLocaleId($locale);
121
122
        foreach ($this->domains as $domain) {
123
            $messages = [];
124
125
            foreach ($catalogue->all($domain) as $message => $translation) {
126
                $messages[$domain . '::' . $message] = $translation;
127
            }
128
129
            $catalogue->replace($messages, $domain);
130
131
            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...
132
                $options = ['default_locale' => $this->defaultLocale];
133
            } else {
134
                $options = [];
135
            }
136
137
            $data = XliffConverter::catalogueToContent($catalogue, $domain, $options);
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...
138
139
            $file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf';
140
141
            try {
142
                file_put_contents($file, $data);
143
144
                $this->client->import()->import($this->projectId, 'symfony_xliff', $file, [
145
                    'locale_id' => $localeId,
146
                    'tags' => $domain,
147
                ]);
148
            } catch (\Throwable $e) {
149
                throw new StorageException($e->getMessage());
150
            } finally {
151
                unlink($file);
152
            }
153
        }
154
    }
155
156
    private function getLocaleId(string $locale): string
157
    {
158
        if (isset($this->localeToIdMapping[$locale])) {
159
            return $this->localeToIdMapping[$locale];
160
        }
161
162
        throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale));
163
    }
164
}
165