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
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-02-21 13:34:24 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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
|
|
|
*
|
2019-10-01 23:37:26 -05: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
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-02-21 13:34:24 -06:00
|
|
|
*
|
2019-10-01 23:37:26 -05: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;
|
2022-11-02 00:25:37 -05:00
|
|
|
use Illuminate\Database\QueryException;
|
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
|
|
|
|
{
|
|
|
|
/** @var AccountMeta $entry */
|
|
|
|
$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) {
|
2018-07-27 23:27:30 -05:00
|
|
|
try {
|
|
|
|
$entry->delete();
|
2022-11-02 00:25:37 -05:00
|
|
|
} catch (QueryException $e) {
|
2021-09-18 03:26:12 -05:00
|
|
|
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage()));
|
2018-07-27 23:27:30 -05:00
|
|
|
}
|
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
|
|
|
}
|