Conditions | 27 |
Paths | 962 |
Total Lines | 98 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
32 | public function getTranslatedEntity(TranslationEntityInterface $entity, $lead = null, Request $request = null) |
||
33 | { |
||
34 | list($translationParent, $translationChildren) = $entity->getTranslations(); |
||
35 | |||
36 | $leadPreference = $chosenLanguage = null; |
||
37 | |||
38 | if (count($translationChildren)) { |
||
39 | if ($translationParent) { |
||
40 | $translationChildren = $translationParent->getTranslationChildren(); |
||
41 | } else { |
||
42 | $translationParent = $entity; |
||
43 | } |
||
44 | |||
45 | // Generate a list of translations |
||
46 | $translations = [$translationParent->getId() => $translationParent->getLanguage()]; |
||
47 | foreach ($translationChildren as $c) { |
||
48 | $translations[$c->getId()] = $c->getLanguage(); |
||
49 | } |
||
50 | |||
51 | // Generate a list of translations for this entity |
||
52 | $translationList = []; |
||
53 | foreach ($translations as $id => $language) { |
||
54 | $core = $this->getTranslationLocaleCore($language); |
||
55 | if (!isset($languageList[$core])) { |
||
56 | $translationList[$core] = []; |
||
57 | } |
||
58 | $translationList[$core][$language] = $id; |
||
59 | } |
||
60 | |||
61 | // Get the contact's preferred language if defined |
||
62 | $languageList = []; |
||
63 | $leadPreference = null; |
||
64 | if ($lead) { |
||
65 | if ($lead instanceof Lead) { |
||
66 | $languageList[$leadPreference] = $lead->getPreferredLocale(); |
||
67 | } elseif (is_array($lead) && isset($lead['preferred_locale'])) { |
||
68 | $languageList[$leadPreference] = $lead['preferred_locale']; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // Check request for language |
||
73 | if (null !== $request) { |
||
74 | $browserLanguages = $request->server->get('HTTP_ACCEPT_LANGUAGE'); |
||
75 | if (!empty($browserLanguages)) { |
||
76 | $browserLanguages = explode(',', $browserLanguages); |
||
77 | if (!empty($browserLanguages)) { |
||
78 | foreach ($browserLanguages as $language) { |
||
79 | if ($pos = false !== strpos($language, ';q=')) { |
||
80 | //remove weights |
||
81 | $language = substr($language, 0, ($pos + 1)); |
||
82 | } |
||
83 | //change - to _ |
||
84 | $language = str_replace('-', '_', $language); |
||
85 | |||
86 | if (!isset($languageList[$language])) { |
||
87 | $languageList[$language] = $language; |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $matchFound = false; |
||
95 | $preferredCore = false; |
||
96 | foreach ($languageList as $language) { |
||
97 | $core = $this->getTranslationLocaleCore($language); |
||
98 | if (isset($translationList[$core])) { |
||
99 | // Does the dialect exist? |
||
100 | if (isset($translationList[$core][$language])) { |
||
101 | // There's a match |
||
102 | $matchFound = $translationList[$core][$language]; |
||
103 | $chosenLanguage = $language; |
||
104 | break; |
||
105 | } elseif (!$preferredCore) { |
||
106 | // This will be the fallback if no matches are found |
||
107 | $preferredCore = $core; |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | if ($matchFound) { |
||
113 | // A translation was found based on language preference |
||
114 | $entity = ($matchFound == $translationParent->getId()) ? $translationParent : $translationChildren[$matchFound]; |
||
115 | } elseif ($preferredCore) { |
||
116 | // Return the best matching language |
||
117 | $bestMatch = array_values($translationList[$preferredCore])[0]; |
||
118 | $entity = ($bestMatch == $translationParent->getId()) ? $translationParent : $translationChildren[$bestMatch]; |
||
119 | $chosenLanguage = $preferredCore; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | // Save the preferred language to the lead's profile |
||
124 | if (!$leadPreference && !empty($chosenLanguage) && $lead instanceof Lead) { |
||
125 | $lead->addUpdatedField('preferred_locale', $chosenLanguage); |
||
126 | } |
||
127 | |||
128 | // Return the translation parent and translated entity |
||
129 | return [$translationParent, $entity]; |
||
130 | } |
||
157 |