Improve code in Jobs.

This commit is contained in:
James Cole
2018-07-22 15:20:45 +02:00
parent a941519db5
commit dbbf0ff5e4
4 changed files with 91 additions and 35 deletions

View File

@@ -23,19 +23,21 @@ use Illuminate\Support\Collection;
use Log;
/**
* Class CreateRecurringTransactions
* Class CreateRecurringTransactions.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreateRecurringTransactions implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/** @var Carbon */
/** @var Carbon The current date */
private $date;
/** @var JournalRepositoryInterface */
/** @var JournalRepositoryInterface Journal repository */
private $journalRepository;
/** @var RecurringRepositoryInterface */
/** @var RecurringRepositoryInterface Recurring transactions repository. */
private $repository;
/** @var array */
/** @var array The users rules. */
private $rules = [];
/**
@@ -61,10 +63,9 @@ class CreateRecurringTransactions implements ShouldQueue
{
Log::debug(sprintf('Now at start of CreateRecurringTransactions() job for %s.', $this->date->format('D d M Y')));
$recurrences = $this->repository->getAll();
$result = [];
Log::debug(sprintf('Count of collection is %d', $recurrences->count()));
$result = [];
/** @var Collection $filtered */
$filtered = $recurrences->filter(
function (Recurrence $recurrence) {
@@ -78,7 +79,6 @@ class CreateRecurringTransactions implements ShouldQueue
if (!isset($result[$recurrence->user_id])) {
$result[$recurrence->user_id] = new Collection;
}
$this->repository->setUser($recurrence->user);
$this->journalRepository->setUser($recurrence->user);
Log::debug(sprintf('Now at recurrence #%d', $recurrence->id));
@@ -112,8 +112,12 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Apply the users rules to newly created journals.
*
* @param User $user
* @param Collection $journals
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function applyRules(User $user, Collection $journals): void
{
@@ -140,6 +144,8 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Helper function for debug information.
*
* @param array $occurrences
*
* @return array
@@ -155,6 +161,8 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Get the users rules.
*
* @param User $user
*
* @return Collection
@@ -172,6 +180,8 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Get the start date of a recurrence.
*
* @param Recurrence $recurrence
*
* @return Carbon
@@ -187,6 +197,8 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Get transaction information from a recurring transaction.
*
* @param Recurrence $recurrence
*
* @return array
@@ -223,11 +235,15 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Check if the occurences should be executed.
*
* @param Recurrence $recurrence
* @param array $occurrences
*
* @return Collection
* @throws \FireflyIII\Exceptions\FireflyException
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function handleOccurrences(Recurrence $recurrence, array $occurrences): Collection
{
@@ -256,7 +272,6 @@ class CreateRecurringTransactions implements ShouldQueue
'tags' => $this->repository->getTags($recurrence),
'user' => $recurrence->user_id,
'notes' => (string)trans('firefly.created_from_recurrence', ['id' => $recurrence->id, 'title' => $recurrence->title]),
// journal data:
'description' => $recurrence->recurrenceTransactions()->first()->description,
'piggy_bank_id' => null,
@@ -264,7 +279,6 @@ class CreateRecurringTransactions implements ShouldQueue
'bill_id' => null,
'bill_name' => null,
'recurrence_id' => (int)$recurrence->id,
// transaction data:
'transactions' => $this->getTransactionData($recurrence),
];
@@ -325,6 +339,8 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Has the recurrence fired today.
*
* @param Recurrence $recurrence
*
* @return bool
@@ -335,6 +351,8 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Has the reuccrence started yet.
*
* @param $recurrence
*
* @return bool
@@ -360,9 +378,13 @@ class CreateRecurringTransactions implements ShouldQueue
}
/**
* Is the info in the recurrence valid?
*
* @param Recurrence $recurrence
*
* @return bool
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function validRecurrence(Recurrence $recurrence): bool
{
@@ -375,7 +397,7 @@ class CreateRecurringTransactions implements ShouldQueue
// has repeated X times.
$journals = $this->repository->getJournals($recurrence, null, null);
if ($recurrence->repetitions !== 0 && $journals->count() >= $recurrence->repetitions) {
if (0 !== $recurrence->repetitions && $journals->count() >= $recurrence->repetitions) {
Log::info(sprintf('Recurrence #%d has run %d times, so will run no longer.', $recurrence->id, $recurrence->repetitions));
return false;

View File

@@ -39,15 +39,15 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/** @var Collection */
/** @var Collection Set of accounts */
private $accounts;
/** @var Carbon */
/** @var Carbon The end date */
private $endDate;
/** @var RuleGroup */
/** @var RuleGroup The rule group */
private $ruleGroup;
/** @var Carbon */
/** @var Carbon The start date */
private $startDate;
/** @var User */
/** @var User The user */
private $user;
/**
@@ -61,6 +61,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get accounts.
*
* @return Collection
*/
public function getAccounts(): Collection
@@ -69,6 +71,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set accounts.
*
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts)
@@ -77,6 +81,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get end date.
*
* @return \Carbon\Carbon
*/
public function getEndDate(): Carbon
@@ -85,6 +91,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set end date.
*
* @param Carbon $date
*/
public function setEndDate(Carbon $date)
@@ -93,6 +101,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get start date.
*
* @return \Carbon\Carbon
*/
public function getStartDate(): Carbon
@@ -101,6 +111,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set start date.
*
* @param Carbon $date
*/
public function setStartDate(Carbon $date)
@@ -109,6 +121,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get user.
*
* @return User
*/
public function getUser(): User
@@ -117,6 +131,8 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set user.
*
* @param User $user
*/
public function setUser(User $user)
@@ -156,7 +172,7 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
*
* @return Collection
*/
protected function collectJournals()
protected function collectJournals(): Collection
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
@@ -171,7 +187,7 @@ class ExecuteRuleGroupOnExistingTransactions extends Job implements ShouldQueue
*
* @return array
*/
protected function collectProcessors()
protected function collectProcessors(): array
{
// Find all rules belonging to this rulegroup
$rules = $this->ruleGroup->rules()

View File

@@ -40,15 +40,15 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/** @var Collection */
/** @var Collection The accounts */
private $accounts;
/** @var Carbon */
/** @var Carbon The end date */
private $endDate;
/** @var Rule */
/** @var Rule The current rule */
private $rule;
/** @var Carbon */
/** @var Carbon The start date */
private $startDate;
/** @var User */
/** @var User The user */
private $user;
/**
@@ -62,6 +62,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get accounts.
*
* @return Collection
*/
public function getAccounts(): Collection
@@ -70,6 +72,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set accounts.
*
* @param Collection $accounts
*/
public function setAccounts(Collection $accounts)
@@ -78,6 +82,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get end date.
*
* @return \Carbon\Carbon
*/
public function getEndDate(): Carbon
@@ -86,6 +92,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set end date.
*
* @param Carbon $date
*/
public function setEndDate(Carbon $date)
@@ -94,6 +102,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get rule.
*
* @return Rule
*/
public function getRule(): Rule
@@ -102,6 +112,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get start date.
*
* @return \Carbon\Carbon
*/
public function getStartDate(): Carbon
@@ -110,6 +122,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set start date.
*
* @param Carbon $date
*/
public function setStartDate(Carbon $date)
@@ -118,6 +132,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Get user.
*
* @return User
*/
public function getUser(): User
@@ -126,6 +142,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
}
/**
* Set user.
*
* @param User $user
*/
public function setUser(User $user)
@@ -137,6 +155,8 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
* Execute the job.
*
* @throws \FireflyIII\Exceptions\FireflyException
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function handle()
{
@@ -170,7 +190,7 @@ class ExecuteRuleOnExistingTransactions extends Job implements ShouldQueue
*
* @return Collection
*/
protected function collectJournals()
protected function collectJournals(): Collection
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);

View File

@@ -37,13 +37,13 @@ class MailError extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
/** @var string */
/** @var string Destination */
protected $destination;
/** @var array */
/** @var array Exception information */
protected $exception;
/** @var string */
/** @var string IP address */
protected $ipAddress;
/** @var array */
/** @var array User information */
protected $userData;
/**
@@ -70,15 +70,13 @@ class MailError extends Job implements ShouldQueue
*/
public function handle()
{
$email = env('SITE_OWNER');
$args = $this->exception;
$args['loggedIn'] = $this->userData['id'] > 0;
$args['user'] = $this->userData;
$args['ip'] = $this->ipAddress;
if ($this->attempts() < 3) {
// mail?
try {
$email = env('SITE_OWNER');
$args = $this->exception;
$args['loggedIn'] = $this->userData['id'] > 0;
$args['user'] = $this->userData;
$args['ip'] = $this->ipAddress;
Mail::send(
['emails.error-html', 'emails.error-text'],
$args,