From c1096c59375f82bb57b9744286931081eaac5be2 Mon Sep 17 00:00:00 2001 From: Derek MacDonald Date: Sat, 3 Nov 2018 09:13:24 -0400 Subject: [PATCH] Refactor duplicate save() conditional --- src/Translatable/Translatable.php | 36 ++++++++++++------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index a481b0b..c7e3578 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -252,31 +252,23 @@ public function setAttribute($key, $value) */ public function save(array $options = []) { - if ($this->exists) { - if ($this->isDirty()) { - // If $this->exists and dirty, parent::save() has to return true. If not, - // an error has occurred. Therefore we shouldn't save the translations. - if (parent::save($options)) { - return $this->saveTranslations(); - } - + if ($this->exists && ! $this->isDirty()) { + // If $this->exists and not dirty, parent::save() skips saving and returns + // false. So we have to save the translations + if ($this->fireModelEvent('saving') === false) { return false; - } else { - // If $this->exists and not dirty, parent::save() skips saving and returns - // false. So we have to save the translations - if ($this->fireModelEvent('saving') === false) { - return false; - } - - if ($saved = $this->saveTranslations()) { - $this->fireModelEvent('saved', false); - $this->fireModelEvent('updated', false); - } + } - return $saved; + if ($saved = $this->saveTranslations()) { + $this->fireModelEvent('saved', false); + $this->fireModelEvent('updated', false); } - } elseif (parent::save($options)) { - // We save the translations only if the instance is saved in the database. + + return $saved; + } + + // We save the translations only if the instance is saved in the database. + if (parent::save($options)) { return $this->saveTranslations(); }