GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( a70b7c...7d482a )
by James
21:49 queued 11:38
created

TransactionRules/Actions/ConvertToWithdrawal.php (1 issue)

1
<?php
2
/**
3
 * ConvertToWithdrawal.php
4
 * Copyright (c) 2018 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
declare(strict_types=1);
23
24
namespace FireflyIII\TransactionRules\Actions;
25
26
27
use FireflyIII\Factory\AccountFactory;
28
use FireflyIII\Models\Account;
29
use FireflyIII\Models\AccountType;
30
use FireflyIII\Models\RuleAction;
31
use FireflyIII\Models\TransactionJournal;
32
use FireflyIII\Models\TransactionType;
33
use Log;
34
35
36
/**
37
 *
38
 * Class ConvertToWithdrawal
39
 */
40
class ConvertToWithdrawal implements ActionInterface
41
{
42
    /** @var RuleAction The rule action */
43
    private $action;
44
45
    /**
46
     * TriggerInterface constructor.
47
     *
48
     * @param RuleAction $action
49
     */
50
    public function __construct(RuleAction $action)
51
    {
52
        $this->action = $action;
53
    }
54
55
    /**
56
     * Execute the action.
57
     *
58
     * @param TransactionJournal $journal
59
     *
60
     * @return bool
61
     * @throws \FireflyIII\Exceptions\FireflyException
62
     */
63
    public function act(TransactionJournal $journal): bool
64
    {
65
        $type = $journal->transactionType->type;
66
        if (TransactionType::WITHDRAWAL === $type) {
67
            // @codeCoverageIgnoreStart
68
            Log::error(sprintf('Journal #%d is already a withdrawal (rule "%s").', $journal->id, $this->action->rule->title));
69
70
            return false;
71
            // @codeCoverageIgnoreEnd
72
        }
73
74
        $destTransactions   = $journal->transactions()->where('amount', '>', 0)->get();
75
        $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
76
77
        // break if count is zero:
78
        if (1 !== $sourceTransactions->count()) {
79
            // @codeCoverageIgnoreStart
80
            Log::error(
81
                vsprintf(
82
                    'Journal #%d has %d source transactions. ConvertToWithdrawal failed. (rule "%s").',
83
                    [$journal->id, $sourceTransactions->count(), $this->action->rule->title]
84
                )
85
            );
86
87
            return false;
88
            // @codeCoverageIgnoreEnd
89
        }
90
        if (0 === $destTransactions->count()) {
91
            // @codeCoverageIgnoreStart
92
            Log::error(
93
                vsprintf(
94
                    'Journal #%d has %d dest transactions. ConvertToWithdrawal failed. (rule "%s").',
95
                    [$journal->id, $destTransactions->count(), $this->action->rule->title]
96
                )
97
            );
98
99
            return false;
100
            // @codeCoverageIgnoreEnd
101
        }
102
103
104
        if (TransactionType::DEPOSIT === $type) {
105
            Log::debug('Going to transform a deposit to a withdrawal.');
106
107
            return $this->convertDeposit($journal);
108
        }
109
        if (TransactionType::TRANSFER === $type) {
110
            Log::debug('Going to transform a transfer to a withdrawal.');
111
112
            return $this->convertTransfer($journal);
113
        }
114
115
        return false; // @codeCoverageIgnore
116
    }
117
118
    /**
119
     * Input is a deposit from A to B
120
     * Is converted to a withdrawal from B to C.
121
     *
122
     * @param TransactionJournal $journal
123
     *
124
     * @return bool
125
     * @throws \FireflyIII\Exceptions\FireflyException
126
     */
127
    private function convertDeposit(TransactionJournal $journal): bool
128
    {
129
        // find or create expense account.
130
        /** @var AccountFactory $factory */
131
        $factory = app(AccountFactory::class);
132
        $factory->setUser($journal->user);
133
134
        $destTransactions   = $journal->transactions()->where('amount', '>', 0)->get();
135
        $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
136
137
        // get the action value, or use the original source revenue name in case the action value is empty:
138
        // this becomes a new or existing expense account.
139
        /** @var Account $source */
140
        $source      = $sourceTransactions->first()->account;
141
        $expenseName = '' === $this->action->action_value ? $source->name : $this->action->action_value;
142
        $expense     = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
143
144
        Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $this->action->action_value, $source->name));
145
        unset($source);
146
147
        // get destination asset account from transaction(s).
148
        /** @var Account $destination */
149
        $destination = $destTransactions->first()->account;
150
151
        // update source transaction(s) to be the original destination account
152
        $journal->transactions()
153
                ->where('amount', '<', 0)
154
                ->update(['account_id' => $destination->id]);
155
156
        // update destination transaction(s) to be new expense account.
157
        $journal->transactions()
158
                ->where('amount', '>', 0)
159
                ->update(['account_id' => $expense->id]);
160
161
        // change transaction type of journal:
162
        $newType                      = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
163
        $journal->transaction_type_id = $newType->id;
164
        $journal->save();
165
166
        Log::debug('Converted deposit to withdrawal.');
167
168
        return true;
169
    }
170
171
    /**
172
     * Input is a transfer from A to B.
173
     * Output is a withdrawal from A to C.
174
     *
175
     * @param TransactionJournal $journal
176
     *
177
     * @return bool
178
     * @throws \FireflyIII\Exceptions\FireflyException
179
     */
180
    private function convertTransfer(TransactionJournal $journal): bool
181
    {
182
        // find or create expense account.
183
        /** @var AccountFactory $factory */
184
        $factory = app(AccountFactory::class);
185
        $factory->setUser($journal->user);
186
187
        $destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
188
189
        // get the action value, or use the original destination name in case the action value is empty:
190
        // this becomes a new or existing expense account.
191
        /** @var Account $destination */
192
        $destination = $destTransactions->first()->account;
193
        $expenseName = '' === $this->action->action_value ? $destination->name : $this->action->action_value;
194
        $expense     = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
195
196
        Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", revenue name is "%s"', $this->action->action_value, $destination->name));
197
        unset($source);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $source seems to be never defined.
Loading history...
198
199
        // update destination transaction(s) to be the expense account
200
        $journal->transactions()
201
                ->where('amount', '>', 0)
202
                ->update(['account_id' => $expense->id]);
203
204
        // change transaction type of journal:
205
        $newType                      = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
206
        $journal->transaction_type_id = $newType->id;
207
        $journal->save();
208
        Log::debug('Converted transfer to withdrawal.');
209
210
        return true;
211
    }
212
}
213