Completed
Pull Request — master (#1)
by Sascha-Oliver
06:50
created

PhraseApp::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\SymfonyStorage\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
    public function __construct(PhraseAppClient $client, string $projectId, array $localeToIdMapping)
34
    {
35
        $this->client = $client;
36
        $this->projectId = $projectId;
37
        $this->localeToIdMapping = $localeToIdMapping;
38
    }
39
40
    public function get($locale, $domain, $key)
41
    {
42
        throw new \BadMethodCallException('Not implemented');
43
    }
44
45
    public function create(Message $message)
46
    {
47
        throw new \BadMethodCallException('Not implemented');
48
    }
49
50
    public function update(Message $message)
51
    {
52
        throw new \BadMethodCallException('Not implemented');
53
    }
54
55
    public function delete($locale, $domain, $key)
56
    {
57
        throw new \BadMethodCallException('Not implemented');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function export(MessageCatalogueInterface $catalogue)
64
    {
65
        $locale = $catalogue->getLocale();
66
        $localeId = $this->getLocaleId($locale);
67
68
        foreach ($catalogue->getDomains() as $domain) {
69
            try {
70
                $response = $this->client->export()->locale($this->projectId, $localeId, 'symfony_xliff', [
71
                    'tag' => $domain
72
                ]);
73
            } catch (\Throwable $e) {
74
                throw new StorageException($e->getMessage());
75
            }
76
77
            $catalogue->addCatalogue(XliffConverter::contentToCatalogue($response, $locale, $domain));
78
        }
79
80
        return $catalogue;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function import(MessageCatalogueInterface $catalogue)
87
    {
88
        $locale = $catalogue->getLocale();
89
        $localeId = $this->getLocaleId($locale);
90
91
        foreach ($catalogue->getDomains() as $domain) {
92
            $data = XliffConverter::catalogueToContent($catalogue, $domain);
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...
93
94
            $file = sys_get_temp_dir() . '/' . $domain . '.' . $locale . '.xlf';
95
96
            try {
97
                file_put_contents($file, $data);
98
99
                $this->client->import()->import($this->projectId, 'symfony_xliff', $file, [
100
                    'locale_id' => $localeId,
101
                    'tags' => $domain,
102
                ]);
103
            } catch (\Throwable $e) {
104
                throw new StorageException($e->getMessage());
105
            } finally {
106
                unlink($file);
107
            }
108
        }
109
    }
110
111
    private function getLocaleId(string $locale): string
112
    {
113
        if (isset($this->localeToIdMapping[$locale])) {
114
            return $this->localeToIdMapping[$locale];
115
        }
116
117
        throw new StorageException(sprintf('Id for locale "%s" has not been configured.', $locale));
118
    }
119
}
120