/** * @param Account $account * @param array $data * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function updateMetadata(Account $account, array $data) { $validFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType']; foreach ($validFields as $field) { $entry = $account->accountMeta()->where('name', $field)->first(); // update if new data is present: if ($entry && isset($data[$field])) { $entry->data = $data[$field]; $entry->save(); } // no entry but data present? if (!$entry && isset($data[$field])) { $metaData = new AccountMeta(['account_id' => $account->id, 'name' => $field, 'data' => $data[$field]]); $metaData->save(); } } }
/** * @param Account $account * @param array $data * */ protected function updateMetadata(Account $account, array $data) { foreach ($this->validFields as $field) { $entry = $account->accountMeta()->where('name', $field)->first(); if (isset($data[$field])) { // update if new data is present: if (!is_null($entry)) { $entry->data = $data[$field]; $entry->save(); continue; } $metaData = new AccountMeta(['account_id' => $account->id, 'name' => $field, 'data' => $data[$field]]); $metaData->save(); } } }