Completed
Push — master ( bdaaf9...c29061 )
by Tobias
09:51
created

StorageService   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 2
dl 0
loc 237
ccs 0
cts 93
cp 0
rs 9.8
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A download() 0 4 1
A upload() 0 4 1
A sync() 0 15 3
A mergeDown() 0 4 1
A mergeUp() 0 4 1
A syncAndFetchMessage() 0 12 2
A get() 0 11 3
A getFromStorages() 0 11 3
A create() 0 6 2
A createStorages() 0 11 3
A update() 0 6 2
A updateStorages() 0 11 3
A delete() 0 6 2
A deleteFromStorages() 0 6 2
A addLocalStorage() 0 6 1
A addRemoteStorage() 0 6 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\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) {
107
            // If message is not in remote storages
108
            $message = $this->getFromStorages($this->localStorages, $locale, $domain, $key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $message is correct as $this->getFromStorages($...$locale, $domain, $key) (which targets Translation\Bundle\Servi...vice::getFromStorages()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
        }
110
111
        $this->updateStorages($this->localStorages, $message);
0 ignored issues
show
Bug introduced by
It seems like $message can be null; however, updateStorages() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($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
     * Create all configured storages with this message. This will not overwrite
155
     * existing message.
156
     *
157
     * {@inheritdoc}
158
     */
159
    public function create(Message $message)
160
    {
161
        foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
162
            $this->createStorages($storages, $message);
163
        }
164
    }
165
166
    /**
167
     * @param Storage[] $storages
168
     * @param Message   $message
169
     */
170
    private function createStorages($storages, Message $message)
171
    {
172
        // Validate if message actually has data
173
        if (empty((array) $message)) {
174
            return;
175
        }
176
177
        foreach ($storages as $storage) {
178
            $storage->update($message);
179
        }
180
    }
181
182
    /**
183
     * Update all configured storages with this message. If messages does not exist
184
     * it will be created.
185
     *
186
     * {@inheritdoc}
187
     */
188
    public function update(Message $message)
189
    {
190
        foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
191
            $this->updateStorages($storages, $message);
192
        }
193
    }
194
195
    /**
196
     * @param Storage[] $storages
197
     * @param Message   $message
198
     */
199
    private function updateStorages($storages, Message $message)
200
    {
201
        // Validate if message actually has data
202
        if (empty((array) $message)) {
203
            return;
204
        }
205
206
        foreach ($storages as $storage) {
207
            $storage->update($message);
208
        }
209
    }
210
211
    /**
212
     * Delete the message form all storages.
213
     *
214
     * {@inheritdoc}
215
     */
216
    public function delete($locale, $domain, $key)
217
    {
218
        foreach ([$this->localStorages, $this->remoteStorages] as $storages) {
219
            $this->deleteFromStorages($storages, $locale, $domain, $key);
220
        }
221
    }
222
223
    /**
224
     * @param Storage[] $storages
225
     * @param string    $locale
226
     * @param string    $domain
227
     * @param string    $key
228
     */
229
    private function deleteFromStorages($storages, $locale, $domain, $key)
230
    {
231
        foreach ($storages as $storage) {
232
            $storage->delete($locale, $domain, $key);
233
        }
234
    }
235
236
    /**
237
     * @param Storage $localStorage
238
     *
239
     * @return StorageService
240
     */
241
    public function addLocalStorage(Storage $localStorage)
242
    {
243
        $this->localStorages[] = $localStorage;
244
245
        return $this;
246
    }
247
248
    /**
249
     * @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...
250
     *
251
     * @return StorageService
252
     */
253
    public function addRemoteStorage(Storage $remoteStorage)
254
    {
255
        $this->remoteStorages[] = $remoteStorage;
256
257
        return $this;
258
    }
259
}
260