firefly-iii/app/Services/Internal/Destroy/AccountDestroyService.php

71 lines
2.3 KiB
PHP
Raw Normal View History

2018-02-21 11:42:15 -06:00
<?php
/**
* AccountDestroyService.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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Services\Internal\Destroy;
use DB;
2018-03-02 09:31:02 -06:00
use Exception;
2018-02-21 11:42:15 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\Transaction;
2018-03-02 09:31:02 -06:00
use FireflyIII\Models\TransactionJournal;
2018-02-21 11:42:15 -06:00
use Log;
/**
* Class AccountDestroyService
*/
class AccountDestroyService
{
/**
* @param Account $account
* @param Account|null $moveTo
*
2018-03-25 02:01:43 -05:00
* @return void
2018-02-21 11:42:15 -06:00
*/
2018-03-25 02:01:43 -05:00
public function destroy(Account $account, ?Account $moveTo): void
2018-02-21 11:42:15 -06:00
{
if (null !== $moveTo) {
DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]);
}
Log::debug('Now trigger account delete response #' . $account->id);
/** @var Transaction $transaction */
foreach ($account->transactions()->get() as $transaction) {
Log::debug('Now at transaction #' . $transaction->id);
2018-03-02 09:31:02 -06:00
/** @var TransactionJournal $journal */
2018-02-21 11:42:15 -06:00
$journal = $transaction->transactionJournal()->first();
if (null !== $journal) {
Log::debug('Call for deletion of journal #' . $journal->id);
2018-03-02 09:31:02 -06:00
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
$service->destroy($journal);
2018-02-21 11:42:15 -06:00
}
}
try {
$account->delete();
2018-03-02 09:31:02 -06:00
} catch (Exception $e) { // @codeCoverageIgnore
2018-03-10 15:38:20 -06:00
Log::error(sprintf('Could not delete account: %s', $e->getMessage())); // @codeCoverageIgnore
2018-02-21 11:42:15 -06:00
}
}
2018-03-05 12:35:58 -06:00
}