From 99b3e24836a3dcd2f4d26fb847b76e1d626270b0 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 12 Aug 2016 15:50:52 +0200 Subject: [PATCH] Code optimalization --- app/Console/Commands/EncryptFile.php | 2 +- app/Http/Requests/SplitJournalFormRequest.php | 66 ++++++++++++------- app/Import/ImportStorage.php | 2 +- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/app/Console/Commands/EncryptFile.php b/app/Console/Commands/EncryptFile.php index 1039c8ec80..174cc3917e 100644 --- a/app/Console/Commands/EncryptFile.php +++ b/app/Console/Commands/EncryptFile.php @@ -59,7 +59,7 @@ class EncryptFile extends Command } $content = file_get_contents($file); $content = Crypt::encrypt($content); - $newName = e($this->argument('key')) . '.upload'; + $newName = e(strval($this->argument('key'))) . '.upload'; $path = storage_path('upload') . '/' . $newName; file_put_contents($path, $content); diff --git a/app/Http/Requests/SplitJournalFormRequest.php b/app/Http/Requests/SplitJournalFormRequest.php index 2a333258e6..7e49abb331 100644 --- a/app/Http/Requests/SplitJournalFormRequest.php +++ b/app/Http/Requests/SplitJournalFormRequest.php @@ -49,25 +49,9 @@ class SplitJournalFormRequest extends Request 'interest_date' => $this->get('interest_date') ? new Carbon($this->get('interest_date')) : null, 'book_date' => $this->get('book_date') ? new Carbon($this->get('book_date')) : null, 'process_date' => $this->get('process_date') ? new Carbon($this->get('process_date')) : null, - 'transactions' => [], + 'transactions' => $this->getTransactionData(), ]; - // description is leading because it is one of the mandatory fields. - foreach ($this->get('description') as $index => $description) { - $transaction = [ - 'description' => $description, - 'amount' => round($this->get('amount')[$index], 2), - 'budget_id' => $this->get('budget_id')[$index] ? intval($this->get('budget_id')[$index]) : 0, - 'category' => $this->get('category')[$index] ?? '', - 'source_account_id' => isset($this->get('source_account_id')[$index]) ? intval($this->get('source_account_id')[$index]) : intval($this->get('journal_source_account_id')), - 'source_account_name' => $this->get('source_account_name')[$index] ?? '', - 'piggy_bank_id' => isset($this->get('piggy_bank_id')[$index]) ? intval($this->get('piggy_bank_id')[$index]) : 0, - 'destination_account_id' => isset($this->get('destination_account_id')[$index]) ? intval($this->get('destination_account_id')[$index]) : intval($this->get('journal_destination_account_id')), - 'destination_account_name' => $this->get('destination_account_name')[$index] ?? '', - ]; - $data['transactions'][] = $transaction; - } - return $data; } @@ -87,14 +71,46 @@ class SplitJournalFormRequest extends Request 'interest_date' => 'date', 'book_date' => 'date', 'process_date' => 'date', - - 'description.*' => 'required|between:1,255', - 'destination_account_id.*' => 'numeric|belongsToUser:accounts,id', - 'destination_account_name.*' => 'between:1,255', - 'amount.*' => 'required|numeric', - 'budget_id.*' => 'belongsToUser:budgets,id', - 'category.*' => 'between:1,255', - 'piggy_bank_id.*' => 'between:1,255', + 'description.*' => 'required|between:1,255', + 'destination_account_id.*' => 'numeric|belongsToUser:accounts,id', + 'destination_account_name.*' => 'between:1,255', + 'amount.*' => 'required|numeric', + 'budget_id.*' => 'belongsToUser:budgets,id', + 'category.*' => 'between:1,255', + 'piggy_bank_id.*' => 'between:1,255', ]; } + + /** + * @return array + */ + private function getTransactionData(): array + { + $return = []; + // description is leading because it is one of the mandatory fields. + foreach ($this->get('description') as $index => $description) { + $transaction = [ + 'description' => $description, + 'amount' => round($this->get('amount')[$index], 2), + 'budget_id' => $this->get('budget_id')[$index] ? intval($this->get('budget_id')[$index]) : 0, + 'category' => $this->get('category')[$index] ?? '', + 'source_account_id' => isset($this->get('source_account_id')[$index]) + ? intval($this->get('source_account_id')[$index]) + : intval( + $this->get('journal_source_account_id') + ), + 'source_account_name' => $this->get('source_account_name')[$index] ?? '', + 'piggy_bank_id' => isset($this->get('piggy_bank_id')[$index]) ? intval($this->get('piggy_bank_id')[$index]) : 0, + 'destination_account_id' => isset($this->get('destination_account_id')[$index]) + ? intval($this->get('destination_account_id')[$index]) + : intval( + $this->get('journal_destination_account_id') + ), + 'destination_account_name' => $this->get('destination_account_name')[$index] ?? '', + ]; + $return[] = $transaction; + } + + return $return; + } } diff --git a/app/Import/ImportStorage.php b/app/Import/ImportStorage.php index 4ebcc5b8ce..e016e93d24 100644 --- a/app/Import/ImportStorage.php +++ b/app/Import/ImportStorage.php @@ -232,7 +232,7 @@ class ImportStorage $one = Transaction::create($sourceData); $two = Transaction::create($destinationData); Log::debug('Created transaction 1', ['id' => $one->id, 'account' => $one->account_id, 'account_name' => $accounts['source']->name]); - Log::debug('Created transaction 2', ['id' => $two->id, 'account' => $two->account_id, 'account_name' => $destination->name]); + Log::debug('Created transaction 2', ['id' => $two->id, 'account' => $two->account_id, 'account_name' => $accounts['destination']->name]); $journal->completed = 1; $journal->save();