| Total Complexity | 50 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Translator 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.
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 Translator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Translator implements ArrayAccess, JsonSerializable |
||
| 10 | { |
||
| 11 | protected $model; |
||
| 12 | protected $attributes = []; |
||
| 13 | protected $locale; |
||
| 14 | |||
| 15 | public function __construct(Model $model) |
||
| 16 | { |
||
| 17 | if (!$model->relationLoaded('translations')) { |
||
| 18 | $model->load('translations'); |
||
| 19 | } |
||
| 20 | |||
| 21 | $this->model = $model; |
||
| 22 | $this->locale = \Illuminate\Support\Facades\Config::get('sitec.facilitador.multilingual.default', 'en'); |
||
| 23 | $attributes = []; |
||
| 24 | |||
| 25 | foreach ($this->model->getAttributes() as $attribute => $value) { |
||
| 26 | $attributes[$attribute] = [ |
||
| 27 | 'value' => $value, |
||
| 28 | 'locale' => $this->locale, |
||
| 29 | 'exists' => true, |
||
| 30 | 'modified' => false, |
||
| 31 | ]; |
||
| 32 | } |
||
| 33 | |||
| 34 | $this->attributes = $attributes; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function translate($locale = null, $fallback = true) |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Save changes made to the translator attributes. |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public function save() |
||
| 54 | { |
||
| 55 | $attributes = $this->getModifiedAttributes(); |
||
| 56 | $savings = []; |
||
| 57 | |||
| 58 | foreach ($attributes as $key => $attribute) { |
||
| 59 | if ($attribute['exists']) { |
||
| 60 | $translation = $this->getTranslationModel($key); |
||
| 61 | } else { |
||
| 62 | $translation = TranslationFacade::model('Translation')->where('table_name', $this->model->getTable()) |
||
|
|
|||
| 63 | ->where('column_name', $key) |
||
| 64 | ->where('foreign_key', $this->model->getKey()) |
||
| 65 | ->where('locale', $this->locale) |
||
| 66 | ->first(); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (is_null($translation)) { |
||
| 70 | $translation = TranslationFacade::model('Translation'); |
||
| 71 | } |
||
| 72 | |||
| 73 | $translation->fill( |
||
| 74 | [ |
||
| 75 | 'table_name' => $this->model->getTable(), |
||
| 76 | 'column_name' => $key, |
||
| 77 | 'foreign_key' => $this->model->getKey(), |
||
| 78 | 'value' => $attribute['value'], |
||
| 79 | 'locale' => $this->locale, |
||
| 80 | ] |
||
| 81 | ); |
||
| 82 | |||
| 83 | $savings[] = $translation->save(); |
||
| 84 | |||
| 85 | $this->attributes[$key]['locale'] = $this->locale; |
||
| 86 | $this->attributes[$key]['exists'] = true; |
||
| 87 | $this->attributes[$key]['modified'] = false; |
||
| 88 | } |
||
| 89 | |||
| 90 | return in_array(false, $savings); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function getModel() |
||
| 94 | { |
||
| 95 | return $this->model; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function getRawAttributes() |
||
| 99 | { |
||
| 100 | return $this->attributes; |
||
| 101 | } |
||
| 102 | |||
| 103 | public function getOriginalAttributes() |
||
| 104 | { |
||
| 105 | return $this->model->getAttributes(); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getOriginalAttribute($key) |
||
| 109 | { |
||
| 110 | return $this->model->getAttribute($key); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function getTranslationModel($key, $locale = null) |
||
| 114 | { |
||
| 115 | return $this->model->getRelation('translations') |
||
| 116 | ->where('column_name', $key) |
||
| 117 | ->where('locale', $locale ? $locale : $this->locale) |
||
| 118 | ->first(); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function getModifiedAttributes() |
||
| 122 | { |
||
| 123 | return collect($this->attributes)->where('modified', 1)->all(); |
||
| 124 | } |
||
| 125 | |||
| 126 | protected function translateAttribute($attribute, $locale = null, $fallback = true) |
||
| 127 | { |
||
| 128 | list($value, $locale, $exists) = $this->model->getTranslatedAttributeMeta($attribute, $locale, $fallback); |
||
| 129 | |||
| 130 | $this->attributes[$attribute] = [ |
||
| 131 | 'value' => $value, |
||
| 132 | 'locale' => $locale, |
||
| 133 | 'exists' => $exists, |
||
| 134 | 'modified' => false, |
||
| 135 | ]; |
||
| 136 | |||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | protected function translateAttributeToOriginal($attribute) |
||
| 141 | { |
||
| 142 | $this->attributes[$attribute] = [ |
||
| 143 | 'value' => $this->model->attributes[$attribute], |
||
| 144 | 'locale' => \Illuminate\Support\Facades\Config::get('sitec.facilitador.multilingual.default', 'pt_BR'), |
||
| 145 | 'exists' => true, |
||
| 146 | 'modified' => false, |
||
| 147 | ]; |
||
| 148 | |||
| 149 | return $this; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function __get($name) |
||
| 167 | } |
||
| 168 | |||
| 169 | public function __set($name, $value) |
||
| 170 | { |
||
| 171 | $this->attributes[$name]['value'] = $value; |
||
| 172 | |||
| 173 | if (!in_array($name, $this->model->getTranslatableAttributes())) { |
||
| 174 | return $this->model->$name = $value; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->attributes[$name]['modified'] = true; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function offsetGet($offset) |
||
| 183 | } |
||
| 184 | |||
| 185 | public function offsetSet($offset, $value) |
||
| 186 | { |
||
| 187 | $this->attributes[$offset]['value'] = $value; |
||
| 188 | |||
| 189 | if (!in_array($offset, $this->model->getTranslatableAttributes())) { |
||
| 190 | return $this->model->$offset = $value; |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->attributes[$offset]['modified'] = true; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function offsetExists($offset) |
||
| 197 | { |
||
| 198 | return isset($this->attributes[$offset]); |
||
| 199 | } |
||
| 200 | |||
| 201 | public function offsetUnset($offset) |
||
| 202 | { |
||
| 203 | unset($this->attributes[$offset]); |
||
| 204 | } |
||
| 205 | |||
| 206 | public function getLocale() |
||
| 207 | { |
||
| 208 | return $this->locale; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function translationAttributeExists($name) |
||
| 218 | } |
||
| 219 | |||
| 220 | public function translationAttributeModified($name) |
||
| 221 | { |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param array-key $key |
||
| 231 | */ |
||
| 232 | public function createTranslation($key, $value) |
||
| 233 | { |
||
| 234 | if (!isset($this->attributes[$key])) { |
||
| 235 | return false; |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!in_array($key, $this->model->getTranslatableAttributes())) { |
||
| 239 | return false; |
||
| 240 | } |
||
| 241 | |||
| 242 | $translation = TranslationFacade::model('Translation'); |
||
| 243 | $translation->fill( |
||
| 244 | [ |
||
| 245 | 'table_name' => $this->model->getTable(), |
||
| 246 | 'column_name' => $key, |
||
| 247 | 'foreign_key' => $this->model->getKey(), |
||
| 248 | 'value' => $value, |
||
| 249 | 'locale' => $this->locale, |
||
| 250 | ] |
||
| 251 | ); |
||
| 252 | $translation->save(); |
||
| 253 | |||
| 254 | $this->model->getRelation('translations')->add($translation); |
||
| 255 | |||
| 256 | $this->attributes[$key]['exists'] = true; |
||
| 257 | $this->attributes[$key]['value'] = $value; |
||
| 258 | |||
| 259 | return $this->model->getRelation('translations') |
||
| 260 | ->where('key', $key) |
||
| 261 | ->where('locale', $this->locale) |
||
| 262 | ->first(); |
||
| 263 | } |
||
| 264 | |||
| 265 | public function createTranslations(array $translations) |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | public function deleteTranslation($key) |
||
| 273 | { |
||
| 274 | if (!isset($this->attributes[$key])) { |
||
| 275 | return false; |
||
| 276 | } |
||
| 277 | |||
| 278 | if (!$this->attributes[$key]['exists']) { |
||
| 279 | return false; |
||
| 280 | } |
||
| 281 | |||
| 282 | $translations = $this->model->getRelation('translations'); |
||
| 283 | $locale = $this->locale; |
||
| 284 | |||
| 285 | TranslationFacade::model('Translation')->where('table_name', $this->model->getTable()) |
||
| 286 | ->where('column_name', $key) |
||
| 287 | ->where('foreign_key', $this->model->getKey()) |
||
| 288 | ->where('locale', $locale) |
||
| 289 | ->delete(); |
||
| 290 | |||
| 291 | $this->model->setRelation( |
||
| 292 | 'translations', $translations->filter( |
||
| 293 | function ($translation) use ($key, $locale) { |
||
| 294 | return $translation->column_name != $key && $translation->locale != $locale; |
||
| 295 | } |
||
| 296 | ) |
||
| 297 | ); |
||
| 298 | |||
| 299 | $this->attributes[$key]['value'] = null; |
||
| 300 | $this->attributes[$key]['exists'] = false; |
||
| 301 | $this->attributes[$key]['modified'] = false; |
||
| 302 | |||
| 303 | return true; |
||
| 304 | } |
||
| 305 | |||
| 306 | public function deleteTranslations(array $keys) |
||
| 307 | { |
||
| 308 | foreach ($keys as $key) { |
||
| 309 | $this->deleteTranslation($key); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | public function __call($method, array $arguments) |
||
| 314 | { |
||
| 315 | if (!$this->model->hasTranslatorMethod($method)) { |
||
| 316 | throw new \Exception('Call to undefined method Translation\Translator::'.$method.'()'); |
||
| 317 | } |
||
| 318 | |||
| 319 | return call_user_func_array([$this, 'runTranslatorMethod'], [$method, $arguments]); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function runTranslatorMethod($method, array $arguments) |
||
| 329 | } |
||
| 330 | |||
| 331 | public function jsonSerialize() |
||
| 332 | { |
||
| 337 | ); |
||
| 338 | } |
||
| 413 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths