Complex classes like StorageService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StorageService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | final class StorageService implements Storage |
||
29 | { |
||
30 | const DIRECTION_UP = 'up'; |
||
31 | const DIRECTION_DOWN = 'down'; |
||
32 | |||
33 | /** |
||
34 | * @var Storage[] |
||
35 | */ |
||
36 | private $localStorages = []; |
||
37 | |||
38 | /** |
||
39 | * @var Storage[] |
||
40 | */ |
||
41 | private $remoteStorages = []; |
||
42 | |||
43 | /** |
||
44 | * @var CatalogueFetcher |
||
45 | */ |
||
46 | private $catalogueFetcher; |
||
47 | |||
48 | /** |
||
49 | * @var CatalogueWriter |
||
50 | */ |
||
51 | private $catalogueWriter; |
||
52 | |||
53 | /** |
||
54 | * @var Configuration |
||
55 | */ |
||
56 | private $config; |
||
57 | |||
58 | /** |
||
59 | * @param CatalogueFetcher $catalogueFetcher |
||
60 | * @param CatalogueWriter $catalogueWriter |
||
61 | * @param Configuration $config |
||
62 | */ |
||
63 | 4 | public function __construct( |
|
72 | |||
73 | /** |
||
74 | * Download all remote storages into all local storages. |
||
75 | * This will overwrite your local copy. |
||
76 | */ |
||
77 | public function download() |
||
83 | |||
84 | /** |
||
85 | * Synchronize translations with remote. |
||
86 | */ |
||
87 | public function sync($direction = self::DIRECTION_DOWN) |
||
104 | |||
105 | /** |
||
106 | * Download and merge all translations from remote storages down to your local storages. |
||
107 | * Only the local storages will be changed. |
||
108 | */ |
||
109 | public function mergeDown() |
||
122 | |||
123 | /** |
||
124 | * Upload and merge all translations from local storages up to your remote storages. |
||
125 | * Only the remote storages will be changed. |
||
126 | * |
||
127 | * This will overwrite your remote copy. |
||
128 | */ |
||
129 | public function mergeUp() |
||
140 | |||
141 | /** |
||
142 | * Get the very latest version we know of a message. First look at the remote storage |
||
143 | * fall back on the local ones. |
||
144 | * |
||
145 | * @param string $locale |
||
146 | * @param string $domain |
||
147 | * @param string $key |
||
148 | * |
||
149 | * @return null|Message |
||
150 | */ |
||
151 | public function syncAndFetchMessage($locale, $domain, $key) |
||
166 | |||
167 | /** |
||
168 | * Try to get a translation from all the storages, start looking in the first |
||
169 | * local storage and then move on to the remote storages. |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function get($locale, $domain, $key) |
||
183 | |||
184 | /** |
||
185 | * @param Storage[] $storages |
||
186 | * @param string $locale |
||
187 | * @param string $domain |
||
188 | * @param string $key |
||
189 | * |
||
190 | * @return null|Message |
||
191 | */ |
||
192 | private function getFromStorages(array $storages, $locale, $domain, $key) |
||
203 | |||
204 | /** |
||
205 | * Create all configured storages with this message. This will not overwrite |
||
206 | * existing message. |
||
207 | * |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 1 | public function create(Message $message) |
|
211 | { |
||
212 | // Validate if message actually has data |
||
213 | 1 | if (empty((array) $message)) { |
|
214 | return; |
||
215 | } |
||
216 | |||
217 | 1 | foreach ([$this->localStorages, $this->remoteStorages] as $storages) { |
|
218 | /** @var Storage $storage */ |
||
219 | 1 | foreach ($storages as $storage) { |
|
220 | 1 | $storage->create($message); |
|
221 | 1 | } |
|
222 | 1 | } |
|
223 | 1 | } |
|
224 | |||
225 | /** |
||
226 | * Update all configured storages with this message. If messages does not exist |
||
227 | * it will be created. |
||
228 | * |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 1 | public function update(Message $message) |
|
232 | { |
||
233 | 1 | foreach ([$this->localStorages, $this->remoteStorages] as $storages) { |
|
234 | 1 | $this->updateStorages($storages, $message); |
|
235 | 1 | } |
|
236 | 1 | } |
|
237 | |||
238 | /** |
||
239 | * @param Storage[] $storages |
||
240 | * @param Message $message |
||
241 | */ |
||
242 | 1 | private function updateStorages(array $storages, Message $message) |
|
243 | { |
||
244 | // Validate if message actually has data |
||
245 | 1 | if (empty((array) $message)) { |
|
246 | return; |
||
247 | } |
||
248 | |||
249 | 1 | foreach ($storages as $storage) { |
|
250 | 1 | $storage->update($message); |
|
251 | 1 | } |
|
252 | 1 | } |
|
253 | |||
254 | /** |
||
255 | * Delete the message form all storages. |
||
256 | * |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 1 | public function delete($locale, $domain, $key) |
|
260 | { |
||
261 | 1 | foreach ([$this->localStorages, $this->remoteStorages] as $storages) { |
|
262 | /** @var Storage $storage */ |
||
263 | 1 | foreach ($storages as $storage) { |
|
264 | 1 | $storage->delete($locale, $domain, $key); |
|
265 | 1 | } |
|
266 | 1 | } |
|
267 | 1 | } |
|
268 | |||
269 | /** |
||
270 | * @param Storage $localStorage |
||
271 | * |
||
272 | * @return StorageService |
||
273 | */ |
||
274 | 4 | public function addLocalStorage(Storage $localStorage) |
|
280 | |||
281 | /** |
||
282 | * @param Storage $remoteStorage |
||
283 | * |
||
284 | * @return StorageService |
||
285 | */ |
||
286 | public function addRemoteStorage(Storage $remoteStorage) |
||
292 | |||
293 | /** |
||
294 | * Download catalogues from all storages. |
||
295 | * |
||
296 | * @return MessageCatalogue[] |
||
297 | */ |
||
298 | private function doDownload() |
||
312 | } |
||
313 |