firefly-iii/app/Factory/AccountMetaFactory.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2018-02-21 13:34:24 -06:00
<?php
2018-05-11 03:08:34 -05:00
2018-02-21 13:34:24 -06:00
/**
* AccountMetaFactory.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-02-21 13:34:24 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-21 13:34:24 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-02-21 13:34:24 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-21 13:34:24 -06:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-02-21 13:34:24 -06:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-02-21 13:34:24 -06:00
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-21 13:34:24 -06:00
namespace FireflyIII\Factory;
2018-07-27 23:27:30 -05:00
use FireflyIII\Models\Account;
2018-02-21 13:34:24 -06:00
use FireflyIII\Models\AccountMeta;
2018-07-27 23:27:30 -05:00
use Log;
2018-02-21 13:34:24 -06:00
/**
* Class AccountMetaFactory
*/
class AccountMetaFactory
{
2018-07-27 23:27:30 -05:00
/**
* Create update or delete meta data.
*
2022-12-29 12:41:57 -06:00
* @param Account $account
* @param string $field
* @param string $value
2018-07-27 23:27:30 -05:00
*
* @return AccountMeta|null
*/
public function crud(Account $account, string $field, string $value): ?AccountMeta
{
2023-01-02 23:48:53 -06:00
/** @var AccountMeta|null $entry */
2018-07-27 23:27:30 -05:00
$entry = $account->accountMeta()->where('name', $field)->first();
// must not be an empty string:
if ('' !== $value) {
// if $data has field and $entry is null, create new one:
if (null === $entry) {
Log::debug(sprintf('Created meta-field "%s":"%s" for account #%d ("%s") ', $field, $value, $account->id, $account->name));
2019-02-13 10:38:41 -06:00
2018-08-24 00:18:33 -05:00
return $this->create(['account_id' => $account->id, 'name' => $field, 'data' => $value]);
2018-07-27 23:27:30 -05:00
}
// if $data has field and $entry is not null, update $entry:
2022-03-29 08:10:05 -05:00
$entry->data = $value;
$entry->save();
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $value, $account->id, $account->name));
2018-07-27 23:27:30 -05:00
}
2018-08-24 00:18:33 -05:00
if ('' === $value && null !== $entry) {
2023-01-02 23:48:53 -06:00
$entry->delete();
2019-02-13 10:38:41 -06:00
2018-08-24 00:18:33 -05:00
return null;
2018-07-27 23:27:30 -05:00
}
return $entry;
}
2021-03-21 03:15:40 -05:00
/**
2022-12-29 12:41:57 -06:00
* @param array $data
2021-03-21 03:15:40 -05:00
*
* @return AccountMeta|null
*/
public function create(array $data): ?AccountMeta
{
return AccountMeta::create($data);
}
2018-03-05 12:35:58 -06:00
}