. */ declare(strict_types=1); namespace FireflyIII\Factory; use Carbon\Carbon; use Exception; use FireflyIII\Models\TransactionJournalMeta; use Log; /** * Class TransactionJournalMetaFactory */ class TransactionJournalMetaFactory { /** * @param array $data * * @return TransactionJournalMeta|null */ public function updateOrCreate(array $data): ?TransactionJournalMeta { $value = $data['data']; /** @var TransactionJournalMeta $entry */ $entry = $data['journal']->transactionJournalMeta()->where('name', $data['name'])->first(); if (is_null($value) && !is_null($entry)) { try { $entry->delete(); } catch (Exception $e) { // @codeCoverageIgnore Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); // @codeCoverageIgnore } return null; } if ($data['data'] instanceof Carbon) { $value = $data['data']->toW3cString(); } if (strlen($value) === 0) { // don't store blank strings. try { $entry->delete(); } catch (Exception $e) { // @codeCoverageIgnore Log::error(sprintf('Could not delete transaction journal meta: %s', $e->getMessage())); // @codeCoverageIgnore } return null; } if (null === $entry) { $entry = new TransactionJournalMeta(); $entry->transactionJournal()->associate($data['journal']); $entry->name = $data['name']; } $entry->data = $value; $entry->save(); return $entry; } }