firefly-iii/app/Factory/AccountMetaFactory.php

102 lines
2.9 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
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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 Exception;
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
{
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
}
2019-02-13 10:38:41 -06:00
2018-02-21 13:34:24 -06:00
/**
* @param array $data
*
* @return AccountMeta|null
*/
public function create(array $data): ?AccountMeta
{
return AccountMeta::create($data);
}
2018-07-27 23:27:30 -05:00
/**
* Create update or delete meta data.
*
* @param Account $account
* @param string $field
* @param string $value
*
* @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:
if (null !== $entry) {
$entry->data = $value;
$entry->save();
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $value, $account->id, $account->name));
}
}
2018-08-24 00:18:33 -05:00
if ('' === $value && null !== $entry) {
2018-07-27 23:27:30 -05:00
try {
$entry->delete();
2018-08-24 00:18:33 -05:00
} catch (Exception $e) { // @codeCoverageIgnore
Log::debug(sprintf('Could not delete entry: %s', $e->getMessage())); // @codeCoverageIgnore
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;
}
2018-03-05 12:35:58 -06:00
}