Completed
Push — master ( 4d50ae...84674b )
by Tobias
06:42
created

StorageService::getFromStorages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 4
crap 12
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\Bundle\Service;
13
14
use Translation\Common\Exception\LogicException;
15
use Translation\Common\Model\Message;
16
use Translation\Common\Storage;
17
18
/**
19
 * A service that you use to handle the storages.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class StorageService implements Storage
24
{
25
    const DIRECTION_UP = 'up';
26
    const DIRECTION_DOWN = 'down';
27
28
    /**
29
     * @var Storage[]
30
     */
31
    private $localStorages = [];
32
33
    /**
34
     * @var Storage[]
35
     */
36
    private $remoteStorages = [];
37
38
    /**
39
     * Download all remote storages into all local storages.
40
     * This will overwrite your local copy.
41
     */
42
    public function download()
43
    {
44
        // TODO
45
    }
46
47
    /**
48
     * Upload all local storages into all remote storages
49
     * This will overwrite your remote copy.
50
     */
51
    public function upload()
52
    {
53
        // TODO
54
    }
55
56
    /**
57
     * Synchronize translations with remote.
58
     */
59
    public function sync($direction = self::DIRECTION_DOWN)
60
    {
61
        switch ($direction) {
62
            case self::DIRECTION_DOWN:
63
                $this->mergeDown();
64
                $this->mergeUp();
65
                break;
66
            case self::DIRECTION_UP:
67
                $this->mergeUp();
68
                $this->mergeDown();
69
                break;
70
            default:
71
                throw new LogicException(sprintf('Direction must be either "up" or "down". Value "%s" was provided', $direction));
72
        }
73
    }
74
75
    /**
76
     * Download and merge all translations from remote storages down to your local storages.
77
     * Only the local storages will be changed.
78
     */
79
    public function mergeDown()
80
    {
81
        // TODO
82
    }
83
84
    /**
85
     * Upload and merge all translations from local storages up to your remote storages.
86
     * Only the remote storages will be changed.
87
     */
88
    public function mergeUp()
89
    {
90
        // TODO
91
    }
92
93
    /**
94
     * Get the very latest version we know of a message. First look at the remote storage
95
     * fall back on the local ones.
96
     *
97
     * @param string $locale
98
     * @param string $domain
99
     * @param string $key
100
     *
101
     * @return null|Message
102
     */
103
    public function syncAndFetchMessage($locale, $domain, $key)
104
    {
105
        $message = $this->getFromStorages($this->remoteStorages, $locale, $domain, $key);
106
        if (!$message) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $message of type string|null is loosely compared to false; 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...
107
            // If message is not in remote storages
108
            $message = $this->getFromStorages($this->localStorages, $locale, $domain, $key);
109
        }
110
111
        $this->updateStorages($this->localStorages, $message);
0 ignored issues
show
Documentation introduced by
$message is of type string|null, but the function expects a object<Translation\Common\Model\Message>.

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...
112
113
        return $message;
114
    }
115
116
    /**
117
     * Try to get a translation from all the storages, start looking in the first
118
     * local storage and then move on to the remote storages.
119
     * {@inheritdoc}
120
     */
121
    public function get($locale, $domain, $key)
122
    {
123
        foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
124
            $value = $this->getFromStorages($storages, $locale, $domain, $key);
125
            if (!empty($value)) {
126
                return $value;
127
            }
128
        }
129
130
        return;
131
    }
132
133
    /**
134
     * @param Storage[] $storages
135
     * @param string    $locale
136
     * @param string    $domain
137
     * @param string    $key
138
     *
139
     * @return null|Message
140
     */
141
    private function getFromStorages($storages, $locale, $domain, $key)
142
    {
143
        foreach ($storages as $storage) {
144
            $value = $storage->get($locale, $domain, $key);
145
            if (!empty($value)) {
146
                return $value;
147
            }
148
        }
149
150
        return;
151
    }
152
153
    /**
154
     * Update all configured storages with this message.
155
     *
156
     * {@inheritdoc}
157
     */
158
    public function update(Message $message)
159
    {
160
        foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
161
            $this->updateStorages($storages, $message);
162
        }
163
    }
164
165
    /**
166
     * @param Storage[] $storages
167
     * @param Message   $message
168
     */
169
    private function updateStorages($storages, Message $message)
170
    {
171
        // Validate if message actually has data
172
        if (empty((array) $message)) {
173
            return;
174
        }
175
176
        foreach ($storages as $storage) {
177
            $storage->update($message);
0 ignored issues
show
Bug introduced by
The call to update() misses some required arguments starting with $domain.
Loading history...
178
        }
179
    }
180
181
    /**
182
     * Delete the message form all storages.
183
     *
184
     * {@inheritdoc}
185
     */
186
    public function delete($locale, $domain, $key)
187
    {
188
        foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
189
            $this->deleteFromStorages($storages, $locale, $domain, $key);
190
        }
191
    }
192
193
    /**
194
     * @param Storage[] $storages
195
     * @param string    $locale
196
     * @param string    $domain
197
     * @param string    $key
198
     */
199
    private function deleteFromStorages($storages, $locale, $domain, $key)
200
    {
201
        foreach ($storages as $storage) {
202
            $storage->delete($locale, $domain, $key);
203
        }
204
    }
205
206
    /**
207
     * @param Storage $localStorage
208
     *
209
     * @return StorageService
210
     */
211
    public function addLocalStorage(Storage $localStorage)
212
    {
213
        $this->localStorages[] = $localStorage;
214
215
        return $this;
216
    }
217
218
    /**
219
     * @param Storage $remoteStorages
0 ignored issues
show
Documentation introduced by
There is no parameter named $remoteStorages. Did you maybe mean $remoteStorage?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
220
     *
221
     * @return StorageService
222
     */
223
    public function addRemoteStorage(Storage $remoteStorage)
224
    {
225
        $this->remoteStorages[] = $remoteStorage;
226
227
        return $this;
228
    }
229
}
230