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

View File

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

View File

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

View File

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