mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Remove unnecessary loops.
This commit is contained in:
@@ -90,17 +90,7 @@ class BudgetFactory
|
|||||||
*/
|
*/
|
||||||
public function findByName(string $name): ?Budget
|
public function findByName(string $name): ?Budget
|
||||||
{
|
{
|
||||||
/** @var Collection $collection */
|
return $this->user->budgets()->where('name', $name)->first();
|
||||||
$collection = $this->user->budgets()->get();
|
|
||||||
// TODO no longer need to loop like this
|
|
||||||
/** @var Budget $budget */
|
|
||||||
foreach ($collection as $budget) {
|
|
||||||
if ($budget->name === $name) {
|
|
||||||
return $budget;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -55,21 +55,7 @@ class CategoryFactory
|
|||||||
*/
|
*/
|
||||||
public function findByName(string $name): ?Category
|
public function findByName(string $name): ?Category
|
||||||
{
|
{
|
||||||
$result = null;
|
return $this->user->categories()->where('name', $name)->first();
|
||||||
/** @var Collection $collection */
|
|
||||||
$collection = $this->user->categories()->get();
|
|
||||||
|
|
||||||
// TODO no longer need to loop like this
|
|
||||||
|
|
||||||
/** @var Category $category */
|
|
||||||
foreach ($collection as $category) {
|
|
||||||
if ($category->name === $name) {
|
|
||||||
$result = $category;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -90,18 +90,7 @@ class PiggyBankFactory
|
|||||||
*/
|
*/
|
||||||
public function findByName(string $name): ?PiggyBank
|
public function findByName(string $name): ?PiggyBank
|
||||||
{
|
{
|
||||||
$set = $this->user->piggyBanks()->get();
|
return $this->user->piggyBanks()->where('name', $name)->first();
|
||||||
|
|
||||||
// TODO no longer need to loop like this
|
|
||||||
|
|
||||||
/** @var PiggyBank $piggy */
|
|
||||||
foreach ($set as $piggy) {
|
|
||||||
if ($piggy->name === $name) {
|
|
||||||
return $piggy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -47,7 +47,6 @@ class RemoveTag implements ActionInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove tag X
|
* Remove tag X
|
||||||
* TODO the filter is no longer necessary.
|
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
@@ -56,12 +55,7 @@ class RemoveTag implements ActionInterface
|
|||||||
{
|
{
|
||||||
// if tag does not exist, no need to continue:
|
// if tag does not exist, no need to continue:
|
||||||
$name = $this->action->action_value;
|
$name = $this->action->action_value;
|
||||||
/** @var Tag $tag */
|
$tag = $journal->user->tags()->where('tag', $name)->first();
|
||||||
$tag = $journal->user->tags()->get()->filter(
|
|
||||||
function (Tag $tag) use ($name) {
|
|
||||||
return $tag->tag === $name;
|
|
||||||
}
|
|
||||||
)->first();
|
|
||||||
|
|
||||||
if (null !== $tag) {
|
if (null !== $tag) {
|
||||||
Log::debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal->id));
|
Log::debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal->id));
|
||||||
|
@@ -22,12 +22,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\TransactionRules\Actions;
|
namespace FireflyIII\TransactionRules\Actions;
|
||||||
|
|
||||||
use FireflyIII\Models\Budget;
|
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\Transaction;
|
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
||||||
use Log;
|
use Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,27 +47,15 @@ class SetBudget implements ActionInterface
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Set budget.
|
* Set budget.
|
||||||
* TODO the filter is no longer necessary.
|
|
||||||
*
|
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function act(TransactionJournal $journal): bool
|
public function act(TransactionJournal $journal): bool
|
||||||
{
|
{
|
||||||
/** @var BudgetRepositoryInterface $repository */
|
|
||||||
$repository = app(BudgetRepositoryInterface::class);
|
|
||||||
$repository->setUser($journal->user);
|
|
||||||
$search = $this->action->action_value;
|
$search = $this->action->action_value;
|
||||||
$budgets = $repository->getActiveBudgets();
|
|
||||||
|
|
||||||
// TODO no longer need to loop like this
|
$budget = $journal->user->budgets()->where('name', $search)->first();
|
||||||
|
|
||||||
$budget = $budgets->filter(
|
|
||||||
static function (Budget $current) use ($search) {
|
|
||||||
return $current->name === $search;
|
|
||||||
}
|
|
||||||
)->first();
|
|
||||||
if (null === $budget) {
|
if (null === $budget) {
|
||||||
Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search));
|
Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user