1
|
|
|
<?php namespace Modules\Translation\Repositories\Cache; |
2
|
|
|
|
3
|
|
|
use Modules\Core\Repositories\Cache\BaseCacheDecorator; |
4
|
|
|
use Modules\Translation\Entities\TranslationTranslation; |
5
|
|
|
use Modules\Translation\Repositories\TranslationRepository; |
6
|
|
|
|
7
|
|
|
class CacheTranslationDecorator extends BaseCacheDecorator implements TranslationRepository |
8
|
|
|
{ |
9
|
|
|
public function __construct(TranslationRepository $recipe) |
10
|
|
|
{ |
11
|
|
|
parent::__construct(); |
12
|
|
|
$this->entityName = 'translation.translations'; |
13
|
|
|
$this->repository = $recipe; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $key |
18
|
|
|
* @param string $locale |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function findByKeyAndLocale($key, $locale = null) |
22
|
|
|
{ |
23
|
|
|
return $this->cache |
24
|
|
|
->tags($this->entityName, 'global') |
25
|
|
|
->rememberForever("{$this->locale}.{$this->entityName}.findByKeyAndLocale.{$key}.{$locale}", |
26
|
|
|
function () use ($key, $locale) { |
27
|
|
|
return $this->repository->findByKeyAndLocale($key, $locale); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function allFormatted() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
return $this->cache |
35
|
|
|
->tags($this->entityName, 'global') |
36
|
|
|
->rememberForever("{$this->locale}.{$this->entityName}.allFormatted", |
37
|
|
|
function () { |
38
|
|
|
return $this->repository->allFormatted(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function saveTranslationForLocaleAndKey($locale, $key, $value) |
44
|
|
|
{ |
45
|
|
|
$this->cache->tags($this->entityName)->flush(); |
46
|
|
|
|
47
|
|
|
return $this->repository->saveTranslationForLocaleAndKey($locale, $key, $value); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function findTranslationByKey($key) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
return $this->cache |
53
|
|
|
->tags($this->entityName, 'global') |
54
|
|
|
->rememberForever("{$this->locale}.{$this->entityName}.findTranslationByKey.{$key}", |
55
|
|
|
function () use ($key) { |
56
|
|
|
return $this->repository->findTranslationByKey($key); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Update the given translation key with the given data |
63
|
|
|
* @param string $key |
64
|
|
|
* @param array $data |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function updateFromImport($key, array $data) |
68
|
|
|
{ |
69
|
|
|
$this->cache->tags($this->entityName)->flush(); |
70
|
|
|
|
71
|
|
|
return $this->repository->updateFromImport($key, $data); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the given value on the given TranslationTranslation |
76
|
|
|
* @param TranslationTranslation $translationTranslation |
77
|
|
|
* @param string $value |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function updateTranslationToValue(TranslationTranslation $translationTranslation, $value) |
81
|
|
|
{ |
82
|
|
|
$this->cache->tags($this->entityName)->flush(); |
83
|
|
|
|
84
|
|
|
return $this->repository->updateTranslationToValue($translationTranslation, $value); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: