Completed
Push — master ( 1146e0...fa5eef )
by Tobias
09:23
created

Loco::export()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 5
nop 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\PlatformAdapter\Loco;
13
14
use FAPI\Localise\Exception\Domain\AssetConflictException;
15
use FAPI\Localise\Exception\Domain\NotFoundException;
16
use FAPI\Localise\LocoClient;
17
use Symfony\Component\Translation\Loader\ArrayLoader;
18
use Symfony\Component\Translation\MessageCatalogueInterface;
19
use Translation\Common\Exception\StorageException;
20
use Translation\Common\Model\Message;
21
use Translation\Common\Storage;
22
use Translation\Common\TransferableStorage;
23
24
/**
25
 * Localize.biz.
26
 *
27
 * @author Tobias Nyholm <[email protected]>
28
 */
29
class Loco implements Storage, TransferableStorage
30
{
31
    /**
32
     * @var LocoClient
33
     */
34
    private $client;
35
36
    /**
37
     * @var array
38
     */
39
    private $domainToProjectId = [];
40
41
    /**
42
     * @param LocoClient $client
43
     * @param array      $domainToProjectId
44
     */
45
    public function __construct(LocoClient $client, array $domainToProjectId)
46
    {
47
        $this->client = $client;
48
        $this->domainToProjectId = $domainToProjectId;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function get($locale, $domain, $key)
55
    {
56
        $projectKey = $this->getApiKey($domain);
57
        $translation = $this->client->translations()->get($projectKey, $key, $locale)->getTranslation();
0 ignored issues
show
Bug introduced by
The method getTranslation does only exist in FAPI\Localise\Resource\Api\Translation\Translation, but not in Psr\Http\Message\ResponseInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
        $meta = [];
59
60
        return new Message($key, $domain, $locale, $translation, $meta);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function create(Message $message)
67
    {
68
        $projectKey = $this->getApiKey($message->getDomain());
69
        $isNewAsset = true;
70
        try {
71
            // Create asset first
72
            $this->client->asset()->create($projectKey, $message->getKey());
73
            $this->client->translations()->create($projectKey, $message->getKey(), $message->getLocale(), $message->getTranslation());
74
        } catch (AssetConflictException $e) {
75
            // This is okey
76
            $isNewAsset = false;
77
        }
78
79
        if ($isNewAsset) {
80
            $this->client->translations()->create(
81
                $projectKey,
82
                $message->getKey(),
83
                $message->getLocale(),
84
                $message->getTranslation()
85
            );
86
        } else {
87
            try {
88
                $this->client->translations()->get(
89
                    $projectKey,
90
                    $message->getKey(),
91
                    $message->getLocale()
92
                );
93
            } catch (NotFoundException $e) {
94
                // Create only if not found.
95
                $this->client->translations()->create(
96
                    $projectKey,
97
                    $message->getKey(),
98
                    $message->getLocale(),
99
                    $message->getTranslation()
100
                );
101
            }
102
        }
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function update(Message $message)
109
    {
110
        $projectKey = $this->getApiKey($message->getDomain());
111
112
        try {
113
            $this->client->translations()->create($projectKey, $message->getKey(), $message->getLocale(), $message->getTranslation());
114
        } catch (NotFoundException $e) {
115
            $this->create($message);
116
        }
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function delete($locale, $domain, $key)
123
    {
124
        $projectKey = $this->getApiKey($domain);
125
        $this->client->translations()->delete($projectKey, $key, $locale);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function export(MessageCatalogueInterface $catalogue)
132
    {
133
        $locale = $catalogue->getLocale();
134
        $loader = new ArrayLoader();
135
        foreach ($this->domainToProjectId as $domain => $projectKey) {
136
            try {
137
                $data = $this->client->export()->locale(
0 ignored issues
show
Bug introduced by
The method export() does not seem to exist on object<FAPI\Localise\LocoClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
                    $projectKey,
139
                    $locale,
140
                    'json',
141
                    ['format' => 'symfony']
142
                );
143
                $array = json_decode($data, true);
144
                $catalogue->addCatalogue(
145
                    $loader->load($array, $locale, $domain)
146
                );
147
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
148
            }
149
        }
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function import(MessageCatalogueInterface $catalogue)
156
    {
157
        $locale = $catalogue->getLocale();
158
        foreach ($this->domainToProjectId as $domain => $projectKey) {
159
            $data = json_encode($catalogue->all($domain));
160
            $this->client->import()->import($projectKey, 'json', $data, ['locale' => $locale, 'async' => 1]);
0 ignored issues
show
Bug introduced by
The method import() does not seem to exist on object<FAPI\Localise\LocoClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
161
        }
162
    }
163
164
    /**
165
     * @param string $domain
166
     *
167
     * @return string
168
     */
169
    private function getApiKey($domain)
170
    {
171
        if (isset($this->domainToProjectId[$domain])) {
172
            return $this->domainToProjectId[$domain];
173
        }
174
175
        throw new StorageException(sprintf('Api key for domain "%s" has not been configured.', $domain));
176
    }
177
}
178