1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Component\Translation\Repository\Doctrine\MongoDB; |
13
|
|
|
|
14
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
15
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
16
|
|
|
use Doctrine\ODM\MongoDB\UnitOfWork; |
17
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
18
|
|
|
use Lug\Component\Resource\Repository\Doctrine\MongoDB\Repository; |
19
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author GeLo <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class TranslatableRepository extends Repository |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var LocaleContextInterface |
28
|
|
|
*/ |
29
|
|
|
private $localeContext; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private $cache; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param DocumentManager $dm |
38
|
|
|
* @param UnitOfWork $uow |
39
|
|
|
* @param ClassMetadata $class |
40
|
|
|
* @param ResourceInterface $resource |
41
|
|
|
* @param LocaleContextInterface $localeContext |
42
|
|
|
*/ |
43
|
2 |
|
public function __construct( |
44
|
|
|
DocumentManager $dm, |
45
|
|
|
UnitOfWork $uow, |
46
|
|
|
ClassMetadata $class, |
47
|
|
|
ResourceInterface $resource, |
48
|
|
|
LocaleContextInterface $localeContext |
49
|
|
|
) { |
50
|
2 |
|
parent::__construct($dm, $uow, $class, $resource); |
51
|
|
|
|
52
|
2 |
|
$this->localeContext = $localeContext; |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
1 |
|
public function createQueryBuilderForCollection() |
59
|
|
|
{ |
60
|
1 |
|
return parent::createQueryBuilderForCollection()->field($this->getProperty('locale'))->in($this->getLocales()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
public function getProperty($property, $root = null) |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
if ($this->cache === null) { |
69
|
1 |
|
$translationMetadata = $this |
70
|
1 |
|
->getDocumentManager() |
71
|
1 |
|
->getClassMetadata($this->getClassMetadata()->getAssociationTargetClass($this->getTranslationAlias())); |
72
|
|
|
|
73
|
1 |
|
$this->cache = array_diff( |
74
|
1 |
|
$this->getProperties($translationMetadata), |
75
|
1 |
|
$this->getProperties($this->getClassMetadata()) |
76
|
1 |
|
); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
if (in_array($this->getRootProperty($property), $this->cache, true)) { |
80
|
1 |
|
$root = $this->getTranslationAlias(); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
return parent::getProperty($property, $root); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
1 |
|
private function getTranslationAlias() |
90
|
|
|
{ |
91
|
1 |
|
return 'translations'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ClassMetadata $metadata |
96
|
|
|
* |
97
|
|
|
* @return string[] |
98
|
|
|
*/ |
99
|
1 |
|
private function getProperties(ClassMetadata $metadata) |
100
|
|
|
{ |
101
|
1 |
|
return array_merge( |
102
|
1 |
|
$metadata->getFieldNames(), |
103
|
1 |
|
array_map(function (array $mapping) { |
104
|
|
|
return $mapping['fieldName']; |
105
|
1 |
|
}, $metadata->getEmbeddedFieldsMappings()) |
106
|
1 |
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $property |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1 |
|
private function getRootProperty($property) |
115
|
|
|
{ |
116
|
1 |
|
return ($pos = strpos($property, '.')) !== false ? substr($property, 0, $pos) : $property; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string[] |
121
|
|
|
*/ |
122
|
1 |
View Code Duplication |
private function getLocales() |
|
|
|
|
123
|
|
|
{ |
124
|
1 |
|
$locales = $this->localeContext->getLocales(); |
125
|
|
|
|
126
|
1 |
|
if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) { |
127
|
1 |
|
$locales[] = $fallbackLocale; |
128
|
1 |
|
} |
129
|
|
|
|
130
|
1 |
|
return $locales; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.