/** * @param AbstractModel $model * @param bool $forceInsert * * @todo think about a way to simplify this I'm sure I overcomplicated the approach based on the code structure * (class ...) the issue is the Responsibility <- the check should not be in the entity manager and not * in the cache layer * * @return mixed */ public function getPersistData(AbstractModel $model, $forceInsert = false) { if ($forceInsert || !$this->modelCache->get($model)) { return $this->queryBuilder->generateInsertForModel($model); } $modelCacheData = $this->queryBuilder->getModelDataCache(); if (isset($modelCacheData[get_class($model)])) { $hasPrimaryKeys = $this->checkForPrimaryKey($modelCacheData, $model); if ($hasPrimaryKeys) { return $this->queryBuilder->generateUpdateForModel($model); } else { return $this->queryBuilder->generateInsertForModel($model); } } return $this->queryBuilder->generateUpdateForModel($model); }